簡體   English   中英

NSDate從字符串錯誤轉換

[英]NSDate from string wrong conversion

我有這個:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:@"yyyy-MM-DD"];
NSDate *offsetDay = [dateFormatter dateFromString:entry.date];

字符串entry.date為2014-02-16,結果offsetDay為2014-01-16。 為什么會這樣?

謝謝。

試試看:

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

如果我沒記錯的話, DD是一年中的一天,而不是您想要的月份中的一天。 您可以在此處找到完整的NSDateFormatter格式化表

嘗試此代碼編輯。

 NSString *entry=@"2014-01-16";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *offsetDay = [dateFormatter dateFromString:entry];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];

NSLog(@"date--%@",[dateFormatter stringFromDate:offsetDay]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM