简体   繁体   English

即使我在NSDateFormatter上设置了时区,为什么stringFromDate NSString仍然具有GMT时间?

[英]Why does stringFromDate NSString still have GMT time even though I set timezone on the NSDateFormatter?

The output for the actual NSDate object ( firstTime ) is what I want it to be (the iOS device's time). 实际NSDate对象( firstTime )的输出是我想要的输出(iOS设备的时间)。 The NSString object ( secondTime ) still seems to be showing the GMT time (which is 4 hours ahead of my local time, EST) even though I set the date formatter ( df ) to the localTimeZone. 即使我将日期格式器( df )设置为localTimeZone ,NSString对象( secondTime )仍似乎显示GMT时间(比我的当地时间EST早4小时)。 Why is it not working? 为什么不起作用?

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
[df setTimeZone:[NSTimeZone localTimeZone]];

NSDate *firstTime = [NSDate dateWithTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT] sinceDate:[NSDate date]];
NSLog(@"%@", firstTime);
NSString *secondTime = [df stringFromDate:firstTime];
NSLog(@"%@", secondTime);

Output: 输出:

FIRST TIME: 2014-05-07 17:41:29 +0000, SECOND TIME: 13:41:29 第一次:2014-05-07 17:41:29 +0000,第二时间:13:41:29

An NSDate does not have a time zone. NSDate没有时区。 Read that five times until you understand it. 读五遍,直到您理解它。

This: 这个:

[NSDate date]

Returns the date that means now. 返回现在的日期。 It does not have a time zone. 它没有时区。 It is not in GMT or in any other time zone. 它不在格林尼治标准时间或任何其他时区。 It does not have one. 它没有一个。 Your code: 您的代码:

[NSDate dateWithTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT] sinceDate:[NSDate date]];

Means a time that isn't now for any user outside GMT. 表示GMT以外的任何用户现在都没有的时间。 I'll repeat that: your NSDate doesn't describe now. 我将重复一遍:您的NSDate现在不再描述。 It's like sitting in San Francisco, looking at your watch which is set in the correct local time and it saying 12:00 then telling everybody "that means it is 4AM because we're 8 hours behind GMT". 这就像坐在旧金山,看着您的手表设置在正确的当地时间,然后说12:00,然后告诉所有人“这是凌晨4点,因为我们比格林尼治标准时间晚8个小时”。

As a result: 结果是:

[df stringFromDate:firstTime]

Will build a string that will show a time that isn't now. 将构建一个字符串,该字符串将显示不是现在的时间。

Stop thinking you're smarter than everybody that's ever worked at Apple and try this: 不要再想自己比以前在苹果公司工作过的每个人都要聪明,请尝试以下方法:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
[df setTimeZone:[NSTimeZone localTimeZone]];

NSString *secondTime = [df stringFromDate:[NSDate date]];
NSLog(@"%@", secondTime);

So that says "give me a string of 'now' in the local time zone". 这样说:“在当地时区给我一串'now'。” Tell me: does it output the correct thing? 告诉我:它输出正确的东西吗?

One should let NSDate capture the real date/time, without trying to perform any timezone adjustment, and then let the NSDateFormatter format the string so that it's represented in the desired timezone. 应该让NSDate捕获真实的日期/时间,而不要尝试进行任何时区调整,然后让NSDateFormatter格式化字符串,以便在所需的时区中表示它。

So, looking at your code: 因此,查看您的代码:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
[df setTimeZone:[NSTimeZone localTimeZone]];

That's fine, though the setting of the timezone to the local timezone is unnecessary. 很好,尽管不需要将时区设置为本地时区。 Date formatters default to the current timezone. 日期格式化程序默认为当前时区。

But then you proceed to attempt to grab the current time, and adjust it: 但是随后您尝试获取当前时间并进行调整:

NSDate *firstTime = [NSDate dateWithTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT] sinceDate:[NSDate date]];
NSLog(@"%@", firstTime);

While I understand why you attempted to do that, this is incorrect. 虽然我了解您为何尝试这样做,但这是不正确的。 No adjustment for timezone should be performed. 请勿调整时区。 So, that assignment of firstTime should be: 因此, firstTime分配应为:

NSDate *firstTime = [NSDate date];
NSLog(@"%@", firstTime);

So, if you did this at 5:41pm (eastern), this NSLog will now report 2014-05-07 21:41:29 +0000 , but that's correct (because the +0000 indicates that it's showing you the time in UTC/GMT/Zulu). 因此,如果您在美国东部时间下午5:41进行此操作,则此NSLog现在将报告2014-05-07 21:41:29 +0000 ,但这是正确的(因为+0000表示它以UTC / GMT /祖鲁语)。

Then, if you want to display that in the local timezone, you can take this un-adjusted [NSDate date] and use it with your formatter, like you did in your question: 然后,如果要在本地时区显示该日期,则可以采用未调整的[NSDate date]并将其与格式化程序一起使用,就像在问题中所做的那样:

NSString *secondTime = [df stringFromDate:firstTime];
NSLog(@"%@", secondTime);

That will then report 17:41:29 , like you expected it to. 然后17:41:29您的预期报告17:41:29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSDateFormatter stringFromDate:返回本地时间而不是UTC / GMT - NSDateFormatter stringFromDate: returning local time instead of UTC / GMT NSDateFormatter和stringFromDate - NSDateFormatter and stringFromDate 即使正确设置了NSTimezone,仍然存在NSDateFormatter结果问题,为什么? - Still having NSDateFormatter result issues even with NSTimezone properly set, why? NSDateformatter stringFromDate在iOS 10.3.1simulator中返回错误的时间字符串 - NSDateformatter stringFromDate returns wrong time string in iOS 10.3.1simulator 为什么我的标签文本不使用NSDateFormatter stringFromDate更改? - Why doesn't my label text change using NSDateFormatter stringFromDate? NSDateFormatter获取stringFromDate错误 - NSDateFormatter gets stringFromDate wrong 为什么我不能使用NSDateFormatter dateFromString从NSString获取Year? - Why can I not get the Year from a NSString using NSDateFormatter dateFromString? 即使我已设置PickerView,也没有任何选项 - PickerView shows up with no options, even though I have set them NSDateFormatter stringFromDate在模拟器中无法在iPad上使用 - NSDateFormatter stringFromDate works in simulator not on iPad 即使配置文件设置为可分发,为什么xcode也会引发此错误? - Why does xcode throw this error even though the profiles are set to distribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM