views:

29

answers:

1

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?

+2  A: 

You are in California (probably) and the date object is GMT. Using the NSCalendar means that the date will be converted from your locale to GMT.

Paul Lynch
yeah,i forget to set zone
Haley