带有 nsdate 的 Nsdateformatter 用于在 iOS 中获取准确的日期
Nsdateformatter with nsdate for getting exact date in iOS
我想从 xml 数据中获取日期,它位于如下所示的字符串中
"Fri, 06 Feb 2015 11:25:00 -0600"
我如何为这种类型的日期设置 nsdateformatter “-0600 是时区”。
请帮助我
参见:ICU Formatting Dates and Times
另外:Date Field SymbolTable.,具体看"Z".
Z 时区:ISO8601 basic hms? / RFC 822 -0800
示例:
NSString *dateString = @"Fri, 06 Feb 2015 11:25:00 -0600";
NSLog(@"dateString: %@", dateString);
输入日期字符串:
dateString: Fri, 06 Feb 2015 11:25:00 -0600
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"E, dd MMM yyyy H:mm:ss Z"];
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"date: %@", date);
输出(格林威治标准时间时区):
date: 2015-02-06 17:25:00 +0000
以特定格式从日期创建字符串:
NSDateFormatter *displayFormatter = [NSDateFormatter new];
[displayFormatter setDateFormat:@"E, dd MMM yyyy H:mm:ss Z"];
[displayFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];
NSString *displayString = [displayFormatter stringFromDate:date];
输出(CST 时区):
displayString: Fri, 06 Feb 2015 11:25:00 -0600
我想从 xml 数据中获取日期,它位于如下所示的字符串中 "Fri, 06 Feb 2015 11:25:00 -0600" 我如何为这种类型的日期设置 nsdateformatter “-0600 是时区”。 请帮助我
参见:ICU Formatting Dates and Times
另外:Date Field SymbolTable.,具体看"Z".
Z 时区:ISO8601 basic hms? / RFC 822 -0800
示例:
NSString *dateString = @"Fri, 06 Feb 2015 11:25:00 -0600";
NSLog(@"dateString: %@", dateString);
输入日期字符串:
dateString: Fri, 06 Feb 2015 11:25:00 -0600
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"E, dd MMM yyyy H:mm:ss Z"];
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"date: %@", date);
输出(格林威治标准时间时区):
date: 2015-02-06 17:25:00 +0000
以特定格式从日期创建字符串:
NSDateFormatter *displayFormatter = [NSDateFormatter new];
[displayFormatter setDateFormat:@"E, dd MMM yyyy H:mm:ss Z"];
[displayFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];
NSString *displayString = [displayFormatter stringFromDate:date];
输出(CST 时区):
displayString: Fri, 06 Feb 2015 11:25:00 -0600