iPhone 中的日期格式化程序问题

发布于 2024-11-28 13:20:43 字数 487 浏览 1 评论 0原文

我得到的 NSDate *newDate 值为 2011-08-12 13:00:00 +0000。 现在,当我尝试通过 dateformatter 将其转换为字符串时,如下所示:

NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd h:mm:ss Z"];

NSString *datestr = [[dateFormatter stringFromDate:newDate] lowercaseString]; 

但是我得到的 datestr 值比 newDate 值多 5:30 小时。我不知道我错在哪里。我得到的 datestr 值为 2011-08-12 18:30:00 + 5:30 (这是不正确的)。

I am getting a NSDate *newDate value as 2011-08-12 13:00:00 +0000.
Now when I try to convert it to a string by dateformatter as follows:

NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd h:mm:ss Z"];

NSString *datestr = [[dateFormatter stringFromDate:newDate] lowercaseString]; 

But the value which I get for datestr is 5:30 hours more than the newDate value. I don't know where I am wrong. I get value for datestr as 2011-08-12 18:30:00 + 5:30 (which is incorrect).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-12-05 13:20:43

它基本上是将时间转换为您当地的时区。如果您不想这样做,只需为日期格式化程序设置时间:

[dateFormatter setTimeZone:[NSTimeZone <use any of the functions below to get your desired one>]];

文档链接:
http://developer.apple.com/library/ mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/

Creating and Initializing Time Zone Objects
+ timeZoneWithAbbreviation:
+ timeZoneWithName:
+ timeZoneWithName:data:
+ timeZoneForSecondsFromGMT:
– initWithName:
– initWithName:data:
+ timeZoneDataVersion
Working with System Time Zones
+ localTimeZone
+ defaultTimeZone
+ setDefaultTimeZone:
+ resetSystemTimeZone
+ systemTimeZone

It is basically converting the time to your local time zone. if you dont want that just set the time aone for date formatter:

[dateFormatter setTimeZone:[NSTimeZone <use any of the functions below to get your desired one>]];

Documentation link:
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/

Creating and Initializing Time Zone Objects
+ timeZoneWithAbbreviation:
+ timeZoneWithName:
+ timeZoneWithName:data:
+ timeZoneForSecondsFromGMT:
– initWithName:
– initWithName:data:
+ timeZoneDataVersion
Working with System Time Zones
+ localTimeZone
+ defaultTimeZone
+ setDefaultTimeZone:
+ resetSystemTimeZone
+ systemTimeZone
挽手叙旧 2024-12-05 13:20:43

看起来它一定是一个时区/UTC 的东西。日期可能以全球/通用时间返回,而您想要的是当地时间。

根据IOS文档: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html - 似乎您可以尝试包含区域设置以指定您想要的时区。

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];

NSLog(@"Date for locale %@: %@",
  [[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);

Looks like it must be a timezone / UTC thing. The date is probably being returned in global / universal time and what you want is the local time.

According to the IOS documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html - seems you could try including a locale in order to specify which time zone you want.

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];

NSLog(@"Date for locale %@: %@",
  [[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文