NSDate is 5 hours off

NSDate defaults to the Universal timezone (aka GMT).

I’m guessing you’re somewhere on the East Coast, 5 hours behind UTC.

Try adding this to your date formatter…

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

[dateFormatter setLocale:[NSLocale currentLocale]];

…and you should see your local time.

If you want to use a specified locale, rather than ‘currentLocale’, create a NSLocale for the relevant locale.

NSLocale *usLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

[dateFormatter setLocale:usLoc];

…actually that’s US (so possibly not Central).

More specific timezone help can be found here…

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

However, if you want to show expiry time, wouldn’t you still want it in the user’s currentLocale?

Leave a Comment