John Bowlinger
John Bowlinger

Reputation: 1579

NSDateFormatter giving me time 4 hours ahead

I'm converting a string "Jun 11, 2012 9:30 PM" to an NSDate and I keep getting 4 hours ahead for some reason. The funny thing is I'm using this same string to feed a UIDatePicker in a detailed view where I have to do the same conversion, and the UIDatePicker renders the time fine. Only when I now try to NSLog it in my main and detailed view do I have problems.

This is in my views :

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM dd, yyyy h:mma"];  
NSDate *dateFromString = [[[NSDate alloc] init]autorelease];

NSLog(@"DATE %@", _date);

dateFromString = [formatter dateFromString:_date];

NSLog(@"NSDATEFROMSTRING %@", dateFromString);

NSLog returns :

2012-06-11 00:02:09.136 LocalDeals[78090:207] DATE Jun 11, 2012 9:30 PM
2012-06-11 00:02:09.137 LocalDeals[78090:207] NSDATEFROMSTRING 2012-06-12 01:30:00 +0000

Even when I add:

[formatter setTimeZone:[NSTimeZone defaultTimeZone]];

I still get the same results from any time I choose. Any ideas? This is on the simulator by the way and yes my Region Format is set to United States.

Upvotes: 2

Views: 1656

Answers (2)

nonopolarity
nonopolarity

Reputation: 151146

The following code will show your time with your time zone:

NSString *_date = @"Jun 11, 2012 9:30 PM";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"MMMM dd, yyyy h:mma"];  
[formatter2 setDateFormat:@"MMMM dd, yyyy h:mma Z"];  

NSDate *dateFromString = [[NSDate alloc] init];

NSLog(@"DATE %@", _date);

dateFromString = [formatter dateFromString:_date];

NSLog(@"NSDATEFROMSTRING %@", dateFromString);
NSLog(@"NSDATEFROMSTRING %@", [formatter stringFromDate:dateFromString]);
NSLog(@"NSDATEFROMSTRING %@", [formatter2 stringFromDate:dateFromString]);

Result:

DATE Jun 11, 2012 9:30 PM
NSDATEFROMSTRING 2012-06-12 04:30:00 +0000
NSDATEFROMSTRING June 11, 2012 9:30PM
NSDATEFROMSTRING June 11, 2012 9:30PM -0700

If you Google for UTC Time now, it does give you a time that is close to the second line in the output, which confirms it is printing out the time as a UTC time.

(We are in different time zone and dateFromString is interpreting the time in the string as your local time.)

Upvotes: 1

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

When you NSLog an NSDate it will print the time as a GMT time zone In order to see the correct data you will have to convert the NSDate to string using stringFromDate

NSDate *dateFromString = [[[NSDate alloc] init]autorelease];

NSLog(@"DATE %@", _date);

//Instead of nslog directly, use this stringFromDate:remindOn
NSString *str = [formatter stringFromDate:dateFromString];
NSLog(@"date is %@", str); //This will log the correct data

The problem you are getting is not in the NSDate but it is in Logging it UPDATE In order to save the data to a file or database i would suggest that you save it like this

NSTimeInterval timeInterval  = [dateFromString timeIntervalSince1970];

Now when you read it again from the database you would do

NSDate *data = [[NSDate alloc] initWithTimeIntervalSince1970:timeInterval]

Upvotes: 2

Related Questions