[NSDate date]是否返回本地日期和时间? [英] Does [NSDate date] return the local date and time?

查看:106
本文介绍了[NSDate date]是否返回本地日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很蠢吗?所有这一次我认为[NSDate date]返回了本地的日期和时间。以后有一些麻烦NSStringformatter / stringFromdate / dateFromString今天我注意到,我的[NSDate日期]返回2011-03-06 11:00:00 +0000。在研究之后,我看到[NSDate date]返回一个始终为GMT的原始日期。

Am I stupid? All this time I thought [NSDate date] returned the local date and time. After having some trouble with NSStringformatter/stringFromdate/dateFromString today I noticed that my [NSDate date] was returning 2011-03-06 11:00:00 +0000. After researching this I see that [NSDate date] returns a raw date which is always GMT.

如果gmt偏移部分总是显示+0000,那么它的作用是什么?另外我不明白[myDate描述]。文档说它应该显示gmt偏移以及dst信息。我得到与[NSDate date]相同的东西。

What purpose does the gmt offset portion serve if it always shows +0000? Also I do not understand [myDate description]. The docs says it is supposed to display gmt offset as well as dst information. I get the same thing as [NSDate date].

我的底线,如果我使用[NSDate date]获取当前日期,并且是在下午2点以后我得到明天的日期,因为我在-10时区。更不用说我今天遇到的问题与NSDateformatter。

Bottom line for me, if I use [NSDate date] to get the current date and it is after 2pm I get tomorrow's date as I am in the -10 time zone. Not to mention the problems I ran into today with NSDateformatter.

我可以正确看到吗?有趣的是,我似乎记得看到[NSDate日期]返回2011-03-06 11:00:00 -36000,或者我认为我看到2011-03-06 11:00:00 -10000。

Am I seeing this correctly? Funny thing is I seem to remember seeing [NSDate date] returning 2011-03-06 11:00:00 -36000, or did I think I was seeing 2011-03-06 11:00:00 -10000.

我可以使用它,但也许有人可以解释这个,帮助我更好地了解NSDate。

I can work with it, but maybe someone can expound on this to help me better understand NSDate.

推荐答案

NSDate返回当前日期。虽然它可以转换为和从不同的地区/时区的字符串格式,NSDate没有时区的内部概念。 NSDates不绑定到任何特定区域。如果你和世界上的任何人要求你的设备同时[NSDate日期],你会得到相等的结果 - 不是因为它总是返回GMT,但是因为它不以时区特定的方式返回。在可可,日期是地球历史上的一个特定时刻。如何写日期与您的日历,您的时区和您的区域设置相关。

NSDate returns the current date. Though it can convert to and from string format in different locales/time zones, NSDate has no internal concept of a time zone. NSDates are not bound to any particular region. If you and anyone else in the world asked your devices to [NSDate date] simultaneously, you'd get equal results — not because it always returns in GMT but because it doesn't return in a time zone-specific manner. In Cocoa, a date is a specific moment in the history of the Earth. How you write a date is related to your calendar, your time zone and your locale.

您没有得到明天的日期,您获得的日期正确,注意它给出一个不同的一天,如果用GMT表示。

You aren't getting tomorrow's date, you're getting the correct date and then noticing it gives a different day if expressed in GMT. It's the correct date, just written differently from what you'd like.

'description'是从NSObject重载的方法。当NSLog一个对象时,内部发生的是调用描述方法并打印返回的字符串。所以你应该从记录 object [object description] 得到相同的结果,因为前者调用描述并打印该字符串,后者调用描述然后调用对结果字符串的描述并打印。 NSStrings返回自己作为描述的结果。

'description' is a method overridden from NSObject. When you NSLog an object, what happens internally is that the description method is called and the string returned is printed. So you should get identical results from logging object and [object description], since the former calls description and prints that string, the latter calls description then calls description on the resulting string and prints that. NSStrings return themselves as the result of description.

它应该是默认行为,但要获得有意义的描述,请尝试:

It should be the default behaviour but to get a meaningful description, try:

NSLog(@"%@", [[NSDate date] descriptionWithCalendarFormat:nil 
           timeZone:[NSTimeZone localTimeZone] locale:[NSLocale currentLocale]]);

如果仍然登录GMT,那么您的设备认为自己是GMT。我不知道模拟器对这种事情的可靠性。

If that still logs in GMT then your device believes itself to be in GMT. I've no idea how reliable the simulator is about that sort of thing.

这篇关于[NSDate date]是否返回本地日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆