Question

I'm sure this is a formatting error on my end. When I try to convert a date to string the conversion is adding 3 hours to the time.

Here's my code:

dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM, dd yyyy HH:mm:ss"];
    NSDate* dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:strToConvert];

Here's some output:

Tuesday, 01 April 2014 | 11:22:12 | KIP_DateManager | convertStringToDate:: | 63 | March, 12 2014 14:22:29

**************************


Tuesday, 01 April 2014 | 11:22:12 | KIP_DateManager | convertStringToDate:: | 68 | 2014-03-12 18:22:29 +0000

**************************


Tuesday, 01 April 2014 | 11:22:12 | KIP_DateManager | convertStringToDate:: | 63 | March, 12 2014 14:16:52

**************************


Tuesday, 01 April 2014 | 11:22:12 | KIP_DateManager | convertStringToDate:: | 68 | 2014-03-12 18:16:52 +0000

**************************


Tuesday, 01 April 2014 | 11:22:12 | KIP_DateManager | convertStringToDate:: | 63 | March, 17 2014 11:14:26

**************************


Tuesday, 01 April 2014 | 11:22:12 | KIP_DateManager | convertStringToDate:: | 68 | 2014-03-17 15:14:26 +0000

Any idea what I am doing wrong?

Was it helpful?

Solution

March, 12 2014 14:22:29

and

2014-03-12 18:22:29 +0000

That's 4 hour difference. Note the parsed date is in +0000 time. You are located in Philadelphia (-0300), with daylight saving time (-1 other hour). Therefore your current time zone is -0400.

Your string doesn't specify a time zone, therefore the system current time zone is used and the date is parsed as 2014-03-12 14:22:29 -0400. However, when you are logging it, you are using [NSDate description] which prints the date with zero time zone.

Your code is giving the correct results but you are testing them wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top