1

On my app, I'm simply getting the current Date as :

[NSDate date];

So, based on the timezone, will it differ? Hence, on 1st Jan 2015 with the device location set in say, Australia, the US Date will still be 2014?

gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • Related question: http://stackoverflow.com/questions/7720060/nsdate-independent-of-timezone?rq=1 – Jessedc May 01 '14 at 06:34

2 Answers2

2

No, your example above will produce a time with an absolute offset from the GMT timezone and will not change based on the device's current location, it is independent.

An instance of NSDate does not include any timezone information. It is essentially an offset from 1/1/2001.

From Apple's NSDate documentation:

NSDate objects represent a single point in time.

Later on it mentions

... the absolute reference date used by NSDate (the first instance of 1 January 2001, GMT).

Timezones (or NSTimezone objects) can be used in conjunction with NSDate and NSCalendar objects that can then be formatted by NSDateFormatter for display.

You may also want to look into [NSLocale systemLocale] and it's sibling methods for more information on how iOS handles locale changes.

A great resource on programming with dates and times is Apple's Date and Time Programming Guide

Jessedc
  • 12,320
  • 3
  • 50
  • 63
  • So if I set a NSDate in Boston for 4:00 , and access it in San Fransisco, will both be 1:00 or will Boston be 4:00 and San Fransisco will be: 1:00 – Logan May 01 '14 at 06:47
  • 1
    Boston is GMT -4. If you call `[NSDate date]` on a device set to GMT-4 at 04:00 GMT (4AM) NSDate will store an absolute value of 08:00 GMT. If you do the same thing in SFO (GMT -8) at the same time 00:00 (Midnight), the internal NSDate value will be the same. – Jessedc May 01 '14 at 21:21
0

If your app has a button that calls [NSDate date] when it's pressed, we are both running the app, you call me on the phone and we hit the buttons at the same time, we will get the same NSDate. If the app calls NSLog (@"%@", [NSDate date]); we will see the same output. We can compare the numbers and see that you pressed the button 0.376 seconds before I did, for example.

Now if you have questions like "what year is it", they can't be answered with the NSDate alone. You have to supply an NSLocale, exactly because the same point in time might still be 2014 in one country and 2015 in another country.

So the NSDate doesn't change if you move your phone from Boston to San Francisco, but the system locale will change, and the time displayed will change. That's one reason why subtle bugs can creep in if you are not careful: Say you set an alarm clock that wakes you up every morning at 8am. If the programmer wrote code that makes the alarm run every 24 hours using NSDate, moving from Boston to San Francisco might mean that you are woken up at the wrong time - 24 hours later than the last alarm, but not 8am.

gnasher729
  • 51,477
  • 5
  • 75
  • 98