How can i get the correct date?

Posted by Haley on Stack Overflow See other posts from Stack Overflow or by Haley
Published on 2010-05-18T05:56:38Z Indexed on 2010/05/18 6:00 UTC
Read the original article Hit count: 222

Filed under:
|

Hi, I have a time of NSString type. i wonder get the day, but i find somethine strange.

     NSString *strTime = @"2010-05-14 16:00:01";
 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 NSDate *oldDate = [formatter dateFromString:strTime];

 unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
 NSCalendar* calendar = [NSCalendar currentCalendar];

 NSDateComponents* components = [calendar components:flags fromDate:oldDate];
 int day = [components day];

When NSString *strTime = @"2010-05-14 16:00:01" the day is 15,and when NSString *strTime = @"2010-05-14 16:00:00" the day is 14.I want to know why?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about nsdate