tags:

views:

51

answers:

1
+1  Q: 

Formatting a date

Edit: Updating post based on Martins comments below

Hello,

I'm just trying to get a date from a string, my code is as follows:

NSString *strStartDateTime = [startTimeArray objectAtIndex:i];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];               
NSDate *dtStartDateTime;
dtStartDateTime = [formatter dateFromString: strStartDateTime];

strStartDateTime has a value of '2010-09-30 15:23:30', but this code returns a value of '2010-09-30 14:23:30 GMT', all I want is '2010-09-30 15:23:30' as per the string.

Is it taking Day light Savings Time into consideration. This is very frustrating, I simply want to convert the NSString to NSDate and its changing the value.

Regards, Stephen

+2  A: 

I don't quite understand your question. What do you mean by "it gives me the GMT value"? What is your input? What is your expected output?

If you have problems with time zone conversions, you could try to set the time zone for the formatter:

[formatter setTimeZone:[NSTimeZone localTimeZone]];

or

[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

As I said, it all depends on what the input and expected output is.

EDIT:

You actually don't have a problem. The date is parsed correctly. You can verify it this way:

NSString *strStartDateTime = [startTimeArray objectAtIndex:i];

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

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  

NSDate *dtStartDateTime = [formatter dateFromString: strStartDateTime];
NSString *parsedDateAsString = [formatter stringFromDate:dtSTartTime];

// parsedDateAsString will have the correct value
Philippe Leybaert
Thanks Philippe, the line [formatter setTimeZone:[NSTimeZone localTimeZone]]; solved.
Stephen
Sorry Philippe, I jump the gun, this didn't actually work.
Stephen