如果我有这个日期:
dtToUse __NSTaggedDate * 2017-12-31 05:00:00 UTC
我使用此方法从字符串中获得:
- (NSDate*) convertStringToDate : (NSString*) strToConvert andTheFormatToUse: (NSString*) strFormat {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:strFormat];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
return [dateFormatter dateFromString:strToConvert];
}
我使用此代码显示:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/YYYY"];
NSString* strDate = [dateFormatter stringFromDate:dtExpiration];
self.txtCurrentField.text = [dateFormatter stringFromDate:dtExpiration];
,但是无论我做什么,年度增量1-因此2017年变为2018
这仅发生在12月31日,每年。我怀疑这是一个时区问题,但我不确定如何解决,因为我正在将时区转换为本地
只需尝试
[dateformatter setDateFormat:@" mm/dd/yyyy"];
而不是
[dateformatter setDateFormat:@" mm/dd/yyyy"];
之后,它将显示2017年12月31日。
Yyyy和Yyyy之间的不同。https://stackoverflow.com/a/15133656/1342266