English 中文(简体)
与NSDate、格式和时间区的联系
原标题:Struggling with NSDate, formats and timezones

The deal is I want a NSDate representation of the date 18th of June 1978. I have tried to init NSDate like

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
self.dateFormatter.timeStyle = NSDateFormatterNoStyle;
self.startDate = [dateFormatter dateFromString:@"19/06/78"];
NSLog(@"%@", self.startDate);

This produces the right date when the iPhone is set to Danish in the International settings. It does not work with a iPhone set to i.e. english/american settings.

Notice the extra day (19) because denmark is in GMT +1 and i think the NSDate defaults to time 23:00:00. That indicates that something i not working as intended.

我希望,上述日期(18/06/1978)代表了一种NSDate Object,而不论时间区、地方和国际环境如何。

如何做到这一点?

<><>Edit>:

So Ole Begemann s answer seemed to resolve my issue. His solution did create the correct date both on a device in USA International settings and en European international settings. But when Apple should review the app the date created was 17th of June 1978. This makes sence since I m setting timezone to GMT +0000. And Cupertino is like GMT -0007 or so. So this would give a day earlier.

因此,我的问题是: 我想18-06-1978年,不管当地情况如何,该装置在身体上或环境上都是明智的。

我想要做的是确定18个起始日期。 1978年,后来在我看来,无论装置的时间区或地点如何,该日期都成为像“780618”(yMMd)这样的严格格式。 一切途径都必须是“780618”,因为这是我评估的关键起点。

因此,我也许会错过这个问题。 我不应试图在任何地方强制使用同一日期,而应接受时间区,并试图将NSDateFormatter混为一谈,以达到正确日期。

So basically: What I want is THE NSDate instance that formats to "780618" everywhere - in any timezone with any international device settings.

如何做到这一点?

最佳回答

我希望,上述日期(18/06/1978)代表了一种NSDate Object,而不论时间区、地方和国际环境如何。

This makes no sense to NSDate. An NSDate always represents a single point in time, so by definition it includes a time and the time zone this time is in (or to be more exact, it stores the time in GMT; the time is only converted to another time zone for display purposes). If you just want the date, you have to make your own definition regarding the time, e.g. to store these dates with a time of midnight GMT.

When setting a date programmatically, you must always set a time zone and, if you use a date formatter, set the formatter s locale and calendar. This ensures that your code is independent of the user s/device s locale settings.

I would do it like this:

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateComponents setYear:1978];
[dateComponents setMonth:6];
[dateComponents setDay:18];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [calendar dateFromComponents:dateComponents];
NSLog(@"%@", date);
问题回答

你可以这样做:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

// Set the locale to en_US_POSIX. This makes the date formatter respect
// the format string regardless of the user s locale.
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
[locale release];

// We want the date in the user s current time zone.
[formatter setTimeZone:[NSTimeZone localTimeZone]];

[formatter setDateFormat:@"dd/MM/yy"];

NSDate *date = [formatter dateFromString:@"18/06/78"];

[formatter release];

NSLog(@"%@", date);

That the date is shown like this 1978-06-17 23:00:00 +0000 is because NSDate s description returns the date in GMT. 17th of June 1978 at 23:00 is 18th of June 1978 00:00 in GMT+1.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签