Date and timezone

1.1k Views Asked by At

In our app, we always need to pick the current Eastern time (EST or EDT).

We set the application timezone as below in the app delegate:

NSTimeZone *ESTTimeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];
[NSTimeZone setDefaultTimeZone:ESTTimeZone];

Also, for the NSDateFormatter, we set the locale as follows:

NSDateFormatter *formatForFileName  = [[NSDateFormatter alloc] init];
[formatForFileName setDateFormat:@"yyyyMMdd"];
NSLocale *uslocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatForFileName setLocale:uslocale];
[uslocale release];

If my iPad is set to correct EST date and time, there is no issue. If I were to log the current date as follows:

NSLog(@"current date : %@",[formatForFileName stringFromDate:[NSDate date]]);

it will display today's date correctly. However, if I change my iPad date to be the next day's date and log the current date, I would want the current EST date and time to be returned. However, [NSDate date] returns me the current iPad date (the next day's date) and time and not the EST date and time.

Is there any way I can get the correct EST date and time using [NSDate date] irrespective of what the user has set on his iPad.

Thanks.

2

There are 2 best solutions below

0
On

I'm not sure if I understand your question. [NSDate date] always creates a date with the date/time the OS thinks is valid at the moment, i.e. the time the device's clock is set to. If you have doubts whether this is indeed the current time, you would need to contact a trustworthy time server on the Internet and get the time from there.

0
On

[NSDate date] returns a date set to the current system date and time. So when the user changes to the next day's date on the system, [NSDate date] will return next day's date. NSDate assumes (rightfully so) that the system date is the correct date.

To display the actual EST date, you will need to query a source (a remote source) other than the system for the date that you want.