简体   繁体   English

NSDateFormatter获取stringFromDate错误

[英]NSDateFormatter gets stringFromDate wrong

I have a string as follows: 我有一个字符串如下:

2012-11-01 2012-11-01

And I want to get a string representing the day from that. 我想要一个字符串来表示从那开始的日期。 So this is how I approach it: 所以这就是我的处理方式:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat: @"yyyy-mm-dd"];

NSDate *date = [dateFormatter dateFromString: @"2012-11-01"];

[dateFormatter setDateFormat: @"EEE"];

NSString *string = [dateFormatter stringFromDate: date];

NSLog(@"%@", string);

And the output is: 输出为:

Sun 太阳

When it really should be: 何时真正应该是:

Thu 星期四

(If my calculations are correct!) (如果我的计算正确!)

So what is the problem here? 那么这是什么问题呢? Is this an internal bug? 这是内部错误吗? Something wrong with my code? 我的代码有问题吗?

Thanks! 谢谢!

Change your first date format string to: 将您的第一个日期格式字符串更改为:

[dateFormatter setDateFormat: @"yyyy-MM-dd"]; // Capital MM for month

It should work then. 那应该可以了。

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

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