NSDateFormatter dateFromString返回错误的月份的日期 [英] NSDateFormatter dateFromString returns date with wrong month
本文介绍了NSDateFormatter dateFromString返回错误的月份的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
for (i=0; i<json.count; i++) {
NSDictionary *bodyDictionary = [json objectAtIndex:i];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:[bodyDictionary objectForKey:@"date"]];
NSNumber* thisVolume = [NSNumber numberWithInteger:[[bodyDictionary objectForKey:@"volume"] integerValue]];
NSTimeInterval timeNumber = [date timeIntervalSince1970];
CGPoint point = CGPointMake(timeNumber, thisVolume.doubleValue);
NSValue *pointValue = [NSValue valueWithCGPoint:point];
[dataArray addObject:pointValue];
}
我在其中循环浏览字典对象的数组-其中之一是日期.我已经检查了输入日期字符串的格式-
Where I cycle through an array of dictionary objects - one of which is a date. I have checked the format of the incoming date string - which is:
2020-07-15 07:01:00
2020-07-15 07:01:00
但是-一旦通过dateformatter,NSDate就会显示为:
However - once it has been through the dateformatter - the NSDate comes out as:
2020-01-15 07:01:00
2020-01-15 07:01:00
推荐答案
您的问题是您在月份和分钟中都使用m
.
Your problem is that you are using m
for both months and minutes.
代替
[formatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"];
您必须使用:
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
点击此处以查看您可以使用的符号的完整列表.
Click here to see the full list of symbols you can use.
对于Swift
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
这篇关于NSDateFormatter dateFromString返回错误的月份的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文