无法格式化 Facebook 创建的日期,Returns NULL 日期
Not able to Format Date created by Facebook, Returns NULL date
Facebook 提供以下格式的日期对象:2010-12-01T21:35:43+0000
我想将此格式转换为 NSDate。
我正在使用以下代码。为什么 return null
.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
dateString = [dateString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateString = [dateString stringByReplacingOccurrencesOfString:@"+" withString:@" +"];
NSLog(@"%@", dateString);
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss '+'ZZZZ"];
NSLog(@"%@",df.dateFormat);
NSDate *date = [df dateFromString:dateString];
NSLog(@"%@ date",date);
只需为您的 dateFormat
使用 yyyy-MM-dd'T'HH:mm:ssZ
并且根本不要乱用字符串操作。
您唯一可能想要做的就是按照 Apple Technical Q&A 1480 中的概述设置 locale
,这样无论设备可能具有何种日历设置,它都可以正常工作:
df.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
只需将代码中的 '+'ZZZZ
替换为 +zzzz
..
NSString *dateString=@"2010-12-01T21:35:43+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
dateString = [dateString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateString = [dateString stringByReplacingOccurrencesOfString:@"+" withString:@" +"];
NSLog(@"%@", dateString);
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss +zzzz"];
NSLog(@"%@",df.dateFormat);
NSDate *date = [df dateFromString:dateString];
NSLog(@"%@ date",date);
输出是:
2016-04-30 14:52:10.118 date[2523:187669] 2010-12-01 21:35:43 +0000
2016-04-30 14:52:10.120 date[2523:187669] yyyy-MM-dd HH:mm:ss +zzzz
2016-04-30 14:52:10.153 date[2523:187669] 2010-12-01 21:35:43 +0000 date
您不需要替换字符串中的任何字符
试试这个代码:
Objective-C
NSString *dateString=@"2010-12-01T21:35:43+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"];
NSDate *date = [df dateFromString:dateString];
NSLog(@"%@ date",date);
Swift:
let dateString: String = "2010-12-01T21:35:43+0000"
let df: NSDateFormatter = NSDateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
let date: NSDate = df.dateFromString(dateString)!
print("date: \(date)")
Facebook 提供以下格式的日期对象:2010-12-01T21:35:43+0000
我想将此格式转换为 NSDate。
我正在使用以下代码。为什么 return null
.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
dateString = [dateString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateString = [dateString stringByReplacingOccurrencesOfString:@"+" withString:@" +"];
NSLog(@"%@", dateString);
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss '+'ZZZZ"];
NSLog(@"%@",df.dateFormat);
NSDate *date = [df dateFromString:dateString];
NSLog(@"%@ date",date);
只需为您的 dateFormat
使用 yyyy-MM-dd'T'HH:mm:ssZ
并且根本不要乱用字符串操作。
您唯一可能想要做的就是按照 Apple Technical Q&A 1480 中的概述设置 locale
,这样无论设备可能具有何种日历设置,它都可以正常工作:
df.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
只需将代码中的 '+'ZZZZ
替换为 +zzzz
..
NSString *dateString=@"2010-12-01T21:35:43+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
dateString = [dateString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateString = [dateString stringByReplacingOccurrencesOfString:@"+" withString:@" +"];
NSLog(@"%@", dateString);
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss +zzzz"];
NSLog(@"%@",df.dateFormat);
NSDate *date = [df dateFromString:dateString];
NSLog(@"%@ date",date);
输出是:
2016-04-30 14:52:10.118 date[2523:187669] 2010-12-01 21:35:43 +0000
2016-04-30 14:52:10.120 date[2523:187669] yyyy-MM-dd HH:mm:ss +zzzz
2016-04-30 14:52:10.153 date[2523:187669] 2010-12-01 21:35:43 +0000 date
您不需要替换字符串中的任何字符
试试这个代码:
Objective-C
NSString *dateString=@"2010-12-01T21:35:43+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"];
NSDate *date = [df dateFromString:dateString];
NSLog(@"%@ date",date);
Swift:
let dateString: String = "2010-12-01T21:35:43+0000"
let df: NSDateFormatter = NSDateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
let date: NSDate = df.dateFromString(dateString)!
print("date: \(date)")