如何将日期字符串“2012-05-03 06:03:00 +0000”转换为 NSDate。我使用下面的代码,但它不起作用:
NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate* date = [datFormatter dateFromString:dateStr];
如何将日期字符串“2012-05-03 06:03:00 +0000”转换为 NSDate。我使用下面的代码,但它不起作用:
NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate* date = [datFormatter dateFromString:dateStr];
NSString *dateStr = @"2012-05-03 06:03:00 +0000";
NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate* date = [datFormatter dateFromString:dateStr];
NSLog(@"date: %@", [datFormatter stringFromDate:date]);
此代码打印date: 2012-05-03 09:03:00 +0300
有用。另外不要忘记设置您要定位的日期格式化程序的时区
可以这样做: -
NSDateFormatter *datFormat = [[NSDateFormatter alloc] init];
[datFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Yout TimeZone"]];
[datFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *string = @"2012-05-03 06:03:00 +0000";
NSDate* date = [datFormat dateFromString:string];