Date [NSDate date] returns local date and time?

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

What purpose does the gmt offset function fulfill if it always shows +0000? Also I do not understand [description of myDate]. The docs say it should display the gmt offset as well as the dst information. I get the same as [NSDate date].

The bottom line is for me, if I use [NSDate date] to get the current date, and this is after 2pm, I get the date tomorrow when I am in the -10 zone. Not to mention the problems that I encountered today with NSDateformatter.

I see it right? It's funny that I seem to remember that I saw the date [NSDate date] 2011-03-06 11:00:00 -36000, or I thought I saw 2011-03-06 11:00:00 -10000.

I can work with him, but maybe someone can explain this to help me better understand NSDate.

+6
iphone cocoa-touch nsdate
source share
2 answers

NSDate returns the current date. Although it can convert to and from string format in different locales / time zones, NSDate does not have an internal time zone concept. NSDates are not tied to any particular area. If you and anyone else in the world asked for your devices on [NSDate date] at the same time, you will get equal results - not because it always returns to GMT, but because it does not return in hourly mode. In Cocoa, the date is a specific moment in the history of the Earth. How you write the date is related to your calendar, your time zone and your language.

You do not receive the date tomorrow, you receive the correct date, and then notice that it gives another day if it is expressed in GMT. This is the correct date, just written differently than you would like.

'description' is a method overridden from NSObject. When you are an NSLog object, what happens inside is that the description method is called and a string is returned. Thus, you should get the same results when registering object and [object description] , since previous calls describe and print this line, the latter calls the description, then calls the description in the received line and prints it. NSStrings are returned as a result of the description.

This should be the default behavior, but to get a meaningful description, try:

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

If it still registers in GMT, your device considers itself GMT. I have no idea how reliable the simulator is in this regard.

+20
source share

In Swift :

NSDate () gives us an agnostic description of the date, it always returns UTC + 0, regardless of the device’s time zone:

 print(NSDate()) 

.descriptionWithLocale (NSLocale.currentLocale ())! gives us a localized description of the 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())!) 
-one
source share

All Articles