NSDate returns wrong date on the first of month

[亡魂溺海] 提交于 2019-11-30 05:32:50

问题


My app used [NSDate date ] function to get current date. Its work fine other days except 1st of every month during AM. i.e Follow Following steps :

  1. Set the system date as 01 - June - 2011 & time between 00.00 midnight to 5.59 AM.
  2. Use following code :
    NSLog(@"Current Date :: %@",[NSDate date]);

The O/P is :: Current Date :: 2011-05-31 19:40:21 +0000

Desired O/P is :: Current Date :: 2011-06-01 00:00:0( i.e.the time which is set ) +0000

Also From 6 AM it works fine.

What is reason for this?

Actually I don't want NSDate in string format but what I want is NSDate date object corresponding to 1st date of current month. For which i use following snippet of code :

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit 
| NSDayCalendarUnit) fromDate:[NSDatedate]];

[comp setDay:1];     

NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];

[gregorian release];

return firstDayOfMonthDate;

Since [NSDate date] returns wrong date, components also contains wrong date. The above code works for all senario except the senario which i posted in beginning of this thread. What should I do?


回答1:


Finally got the solution:

The point is, NSDate objects don't have a time zone associated with them, and a few releases back someone in Apple decided that NSDate's -description method, which is what NSLog uses, should use GMT+0 and NOT the local time zone for output. (I assume someone had a bug where they corrupted their locale setting, and that caused NSDate's -description method to crash, and so bug reports got filed...)

The short version: Use NSDateFormatter to output dates. Don't use -description.




回答2:


Are you running iOS version 4.2? If so, there is some kind of issue with the date method, it does not seem to respect your local time zone. For example, if I run your code in the iOS 4.0 simulator, I get this output:

Current Date :: 2011-07-06 10:43:38 -0400

But if I run it in the iOS 4.2 simulator, I get this:

Current Date :: 2011-07-06 14:44:18 +0000

Here is another SO post that describes this in more detail:

NSDate et al woes on iOS 4.2

If you are just looking for the current date and time in an NSString, I use code that looks like this to accomplish that task:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *result = [formatter stringFromDate:[NSDate date]];
[formatter release];
NSLog(@"Current Date v2 :: %@",result);


来源:https://stackoverflow.com/questions/6596471/nsdate-returns-wrong-date-on-the-first-of-month

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!