【问题标题】:NSDateFormatter dateFromString returns wrong date even when TimeZone details supplied with stringNSDateFormatter dateFromString 返回错误的日期,即使 TimeZone 详细信息提供了字符串
【发布时间】:2015-07-01 10:12:25
【问题描述】:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"EEEE','dd MMM YYYY z"];

NSString *string = @"Thursday,11 Jun 2015 +0530";

NSDate *tempDate = [dateFormat dateFromString:string];

现在当我 NSLog..

NSLog(@"%@",[tempDate description]);

2014-12-24 18:30:00 +0000

NSLog(@"%@",[dateFormat stringFromDate:tempDate]);

IST 2014 年 12 月 25 日星期四

为什么不显示“星期四,2015 年 6 月 11 日 IST”,而是显示“星期四,2014 年 12 月 25 日 IST”?

【问题讨论】:

  • 这里的 x "EEEE','dd MMM YYYY x" 是什么?您可以使用 NSLocal 设置时区

标签: ios objective-c iphone nsdate nsdateformatter


【解决方案1】:
if you change to 'yyyy' will show you result like.

2015 年 6 月 11 日星期四 +0530

NSDateFormatter *dateFormattor = [[NSDateFormatter alloc] init];
[dateFormattor setDateFormat:@"EEEE','dd MMM yyyy zzzz"];
 NSTimeZone *_timezone = [NSTimeZone timeZoneWithName:@"IST"];
[dateFormattor setTimeZone:_timezone];
NSString *string = @"Thursday,11 Jun 2015 +0530";
NSDate *tempDate = [dateFormattor dateFromString:string];
NSLog(@"tempDate is :%@",tempDate);
NSLog(@"%@",[dateFormattor stringFromDate:tempDate]);

印度标准时间 2015 年 6 月 11 日,星期四

在 'z' char 处换行会得到不同的结果。

[dateFormattor setDateFormat:@"EEEE','dd MMM yyyy zzzz"];
  1. z = GMT+0530 IST
  2. zz = GMT+0530 IST
  3. zzz = GMT+0530 IST
  4. zzzz = 印度标准时间 IST
  5. zzzzz = 印度标准时间 IST

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我将 'YYYY' 更改为 'yyyy' 并且成功了。感谢您阅读本文。

    【讨论】:

    【解决方案3】:
    NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
    outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
    outDateFormatter.dateFormat = (your date formate)
    

    试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2010-12-07
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多