Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NSDate for today with 00:00:00 as time

I am writing a categorie in Xcode, that would extend the current NSDate class. I want to add two methods which I use regularly and somehow I can't get them to work properly.

Currently I have this code:

+ (NSDate*) today
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
    NSInteger theDay = [todayComponents day];
    NSInteger theMonth = [todayComponents month];
    NSInteger theYear = [todayComponents year];

    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:theDay]; 
    [components setMonth:theMonth]; 
    [components setYear:theYear];

    NSDate* todayDate = [gregorian dateFromComponents:components];

    [components release];
    [gregorian release];

    return todayDate;
}

I want it to return a date like this: "2010-11-03 00:00:00 +01". But somehow the timezone keeps buggin me, because this code returns "2010-11-02 23:00:00 +0000".

Can anyone tell me how to fix this code to actually return the correct date? Or can I just use this date and my application will convert it itself because of the timezone the machine is set to.

I have to log certain events in my app to a database, which also just uses the [NSDate date] method. Does that mean that the [NSDate date] method also uses the time without timezone information?

EDIT: I think it has something to do with the Daylight savings time bug. The things I see is exactly the same as probably the Clock app has, with the bug making people wake up late. Also, the TimeZone defaults to the TimeZone currently set on your device, so it should stay the same until you change the timezone in your settings screen.

EDIT2: Ok, some more tests:

NSLog(@"CurrentDate: %@", [NSDate date]);
NSLog(@"TZ: %@", [NSTimeZone defaultTimeZone]);

Gives me the following results:

2010-11-03 23:23:49.000 App[8578:207] CurrentDate: 2010-11-03 22:23:49 +0000
2010-11-03 23:23:49.001 App[8578:207] TZ: Europe/Amsterdam (GMT+01:00) offset 3600
like image 537
Wim Haanstra Avatar asked Nov 03 '10 12:11

Wim Haanstra


4 Answers

See Using Time Zones. You'll want to set the calendar's time zone using NSCalendar's -setTimeZone: method before you start asking it for dates.

like image 128
Joshua Nozzi Avatar answered Nov 15 '22 00:11

Joshua Nozzi


This is an interesting question and I worked at a solution for many hours. These are my findings:

NSLog(@"CurrentDate: %@", [NSDate date]);

The code shown above will have the same result as the code shown below:

NSLog(@"CurrentDate: %@", [[NSDate date] description]);

Reading through the NSDate Class Reference produces this documentation on the NSDate's description method.

The representation is not guaranteed to remain constant across different releases of the operating system. To format a date, you should use a date formatter object instead (see NSDateFormatter and Data Formatting Guide)

I also ran across the documentation for descriptionWithLocale: (id) locale:

“Returns a string representation of the receiver using the given locale.”

So, change your code

NSLog(@"CurrentDate: %@", [NSDate date]);

To:

NSLog(@"CurrentDate: %@", [[NSDate date] descriptionWithLocale:[NSLocale currentLocale]]);

Which should result in what you are looking for. And I can also prove that the [NSDate date] really give's the correct date, but is just being displayed with wrong method:

We can use the [today] (Wim Haanstra) to create two dates.

dateLastDay: 2010-11-02 23:59:00 +01
dateToday: 2010-11-03 24:01:00 +01

Then we use the code below to show the two dates:

NSLog(@"CurrentDate: %@", dateLastDay);
NSLog(@"CurrentDate: %@", dateToday);

Or:

NSLog(@"CurrentDate: %@", [dateLastDay description]);
NSLog(@"CurrentDate: %@", [dateToday description]);

The two groups show the same results, like this: "2010-11-02 22:59:00 +0000" and "2010-11-02 23:01:00 +0000". It looks like the two dates have the same ‘day’, but really?

Now we compare the days of the dates:

NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *lastDayComponents = [gregorian components:unitFlags fromDate:dateLastDay];
NSDateComponents *todayComponents = [gregorian components:unitFlags fromDate:dateToday];
NSInteger lastDay = [lastDayComponents day];
NSInteger today = [todayComponents day];

return (lastDay == today) ? YES : NO;

We will get NO! Although the two dates appear to have the same day, month and year, they DON'T. It only appears that way because we displayed them in the wrong way.

like image 23
Maojia Avatar answered Nov 15 '22 00:11

Maojia


Did you try using:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
todayAtOO = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
like image 37
Lucas Avatar answered Nov 15 '22 00:11

Lucas


I'm still trying to figure out exactly why this bug happens, but I do know a solution. Setting the timezone of your NSCalendar to GMT before sending it dateFromComponents: will solve the issue.

[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

PS if there's a different solution that you found based on Joshua's suggestion, could you let us know what it is? It seems like you've solved the issue since you accepted his answer, but it's not really clear what you did. Thanks!

like image 33
yuji Avatar answered Nov 15 '22 00:11

yuji