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?
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?
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
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.