我寫函數,轉換當前時間utc-gmt或gmt -utc。如果msgArrivedDate爲null,函數可以正常工作。如果不是(這意味着,msgArrivedDate來自其他服務,它的劑量不轉換格林尼治標準時間和UTC格式化與NSDateFormatter
JSON解析部分:
NSArray *messageSentTime = [[args valueForKey:@"messageSendDate"] objectAtIndex:0];
for(int i=0 ;i< [messageSentTime count]; i++)
{
//[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]];
NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]);
功能部分:
-(id)timeZoneFormatter:(NSString *)formatType : (NSString *)msgArrivedDate
{
NSDate *date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
if([msgArrivedDate length] > 0)
{
date = [dateFormatter dateFromString:msgArrivedDate];
} else {
date = [NSDate date];
}
if([formatType isEqualToString:@"UTC"])
{
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
}
if([formatType isEqualToString:@"GMT"])
{
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
}
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}
休息回到我在UTC這些值格式:
"2013-09-24 15:03:17", "2013-09-25 12:09:22", "2013-09-25 13:07:45", "2013-09-25 13:08:19", "2013-09-25 14:22:38"
當我打電話的功能,(NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]))
回報:
messageSentTime = (
"2013-09-24 15:03:17",
"2013-09-25 12:09:22",
"2013-09-25 13:07:45",
"2013-09-25 13:08:19",
"2013-09-25 14:22:38")
我覺得我只是缺少在這裏有點問題:(找不到尚未...
UTC和GMT是同一時間。 – neilco
UTC和GMT只是我在函數中使用的參數,GMT意味着在我的作用域= NSTimeZone localTimeZone,UTC = NSTimeZone timeZoneWithAbbreviation:@「UTC」]它們都可以在我的函數上使用null參數正常工作 –
不要使用GMT意味着它不是。使用拼箱或一些這樣的意思是當地時間。 –