开发者

How can i get the correct date?

开发者 https://www.devze.com 2022-12-31 15:42 出处:网络
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\";

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];

 NSDateComponen开发者_如何学Pythonts* 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消