简体   繁体   English

NSDate-dateFromString返回nil

[英]NSDate - dateFromString is returning nil

I have an NSString that has a date. 我有一个带有日期的NSString I'm trying to convert that date to an NSDate . 我正在尝试将该日期转换为NSDate When I do that I get nil fro the NSDate . 当我这样做时,我从NSDate那里得到nil Here is my code: 这是我的代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];

NSLog(@"dateString = %@ date = %@", dateString, date);

Here is the output of the NSLog : 这是NSLog的输出:

dateString = 2015-06-16 date = (null)

What am I doing wrong, and how can I fix it? 我在做什么错,该如何解决?

The date format does not match the date string. 日期格式与日期字符串不匹配。 It needs to be: :@"yyyy-MM-dd" 它必须是::@“ yyyy-MM-dd”

The order and other characters need to match. 顺序和其他字符必须匹配。
yyyy for a four digit year yyyy四位数年份
MM for a two digit month MM两位数月
dd for a two digit day dd为两位数的天

See: ICU Formatting Dates and Times 请参阅: ICU格式化日期和时间

Note: NSLog() uses the NSDate description method which presents date/time referenced to GMT (UTC) and NSDateFormatter defaults to your timezone so the date displayed may be different. 注意: NSLog()使用NSDate description方法,该方法显示参考GMT(UTC)的日期/时间,而NSDateFormatter默认为您的时区,因此显示的日期可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM