How to get the difference between two dates?

I have two dates in the format (MM / dd / yyyy hh: mm: ss: SS). For both dates, I converted two dates to strings using the (stringFromDate) method. But I could not get the difference between them and show them in the console. Please let me know how I should get this? Thank.

+3
source share
4 answers

Example

    NSDate *today = [NSDate date];

NSTimeInterval dateTime;


if ([visitDate isEqualToDate:today])   //visitDate is a NSDate

{

NSLog (@"Dates are equal");

}

dateTime = ([visitDate timeIntervalSinceDate:today] / 86400);  

if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val

{

NSLog (@"Past Date");

}

else 

{   
NSLog (@"Future Date");

}
+3
source

Keep the dates as dates, get the difference between them, then print the difference.

From docs to NSCalendar and assuming gregorian is NSCalendar:

NSDate *startDate = ...;

NSDate *endDate = ...;

unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];

int months = [comps month];

int days = [comps day];
+2
source

- - , / ( , epoch, 01.01.1970).

, , . , .

namespace {
    // Helper class for figuring out things like day of year
    class month_database {
    public:
        month_database () {

            days_into_year[0] = 0;
            for (int i=0; i<11; i++) {
                days_into_year[i+1] = days_into_year[i] + days_in_month[i];
            }
        };

        // Return the start day of the year for the given month (January = month 1).
        int start_day (int month, int year) const {

            // Account for leap years. Actually, this doesn't get the year 1900 or 2100 right,
            // but should be good enough for a while.
            if ( (year % 4) == 0 && month > 2) {
                return days_into_year[month-1] + 1;
            } else {
                return days_into_year[month-1];
            }
        }
    private:
        static int const days_in_month[12];

        // # of days into the year the previous month ends
        int days_into_year[12];
    };
    // 30 days has September, April, June, and November...
    int const month_database::days_in_month[12] = {31, 28, 31, 30,   31, 30, 31, 31,   30, 31, 30, 31};

    month_database month;
}

start_day, , , , . , , . , , .

February 29 in the Gregorian calendar, the most widely used today, is the date it occurs only once every four years, in years evenly divided by 4, such as 1976, 1996, 2000, 2004, 2008, 2012 or 2016. (Except for a century not divisible by 400, such as 1900).

0
source

If you just want the difference in days, you can do it. (Based on mihir mehta answer.)

const NSTimeInterval kSecondsPerDay = 60 * 60 * 24;
- (NSInteger)daysUntilDate:(NSDate *)anotherDate {
    NSTimeInterval secondsUntilExpired = [self timeIntervalSinceDate:anotherDate];
    NSTimeInterval days = secondsUntilExpired / kSecondsPerDay;
    return (NSInteger)days;
}
0
source

Source: https://habr.com/ru/post/1737328/


All Articles