2

Am getting the time format like below

"Wed, 12 Dec 2012 09:08:30 GMT" Now i want to convert it to local time format.Please give me your suggestion

Thanks,

Vijayan.

4

2 回答 2

2

尝试这个:

本地到格林威治标准时间

NSString *dateString = @"Wed, 12 Dec 2012 09:08:30 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM yyyy hh:mma"];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"BST"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
于 2012-12-12T09:24:49.197 回答
2

好吧,我们需要先创建一个NSDate对象形式“Wed, 12 Dec 2012 09:08:30 GMT”

NSString *dateString = @"Wed, 12 Dec 2012 09:08:30 GMT";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"EN"]];
NSDate *date = [dateFormatter dateFromString:dateString];

您可能会发现我设置的区域设置NSDateFormatter与本地化日期有关(月份和日期都是英文)。

现在您可以使用日期格式化程序在本地时区显示 dat:

[dateFormatter setLocale:[NSLocale systemLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *localDateString = [dateFormatter stringFromDate:date];
于 2012-12-12T09:49:56.067 回答