Does [NSDate date] return the local date and time?
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 noti开发者_如何转开发ced 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.
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].
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.
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.
I can work with it, but maybe someone can expound on this to help me better understand NSDate.
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.
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' 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]]);
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.
In Swift:
NSDate() gives us an agnostic date's description, it always returns the UTC+0 date time, independently of the device's time zone:
print(NSDate())
.descriptionWithLocale(NSLocale.currentLocale())! gives us a localized date's description UTC+N date time where N represents the offset (positive or negative) between UTC+0 and the current local date time:
print(NSDate().descriptionWithLocale(NSLocale.currentLocale())!)
精彩评论