Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter returns a date off by a day

In my iPhone app, I am trying to get the EEE MMM dd from a date string received by a web service.

For all dates where time is < 07:00, the NSDateFormatter returns a date off by 1 day. For example: Date to format: 2010-11-17T05:00:00, formatted date result: Tue Nov 16

Here's the code snippet: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat: @"EEE MMM dd"];

NSDate *dateToFormat = [NSDate dateWithNaturalLanguageString:dateStringToFormat locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];

NSString *formattedDateString = [dateFormat stringFromDate:dateToFormat]; [dateFormat release];

What could be the problem?

like image 743
kelkarm Avatar asked Oct 06 '10 21:10

kelkarm


1 Answers

Probably has something to do with time zones. You probably need to set an appropriate value for the timeZone property of your NSDateFormatter object.

like image 87
William Jockusch Avatar answered Sep 28 '22 00:09

William Jockusch