SampathKumar
SampathKumar

Reputation: 2545

How to convert time formatter in iPhone?

Currently i am working in iPhone application, Here i have using NSString to store (2012-07-11 11:49:55 +0000) and using NSDateFormatter to convert the date and time but the output comes 05:19:55, but i want 11:49:55, please any one help me

Here i tried the source code for your reference

NSString *dateString = [NSString stringwithformat:%@,(2012-07-11 11:49:55 +0000)];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"The Current Time is %@",[dateFormatter stringFromDate:dateString]);
[dateFormatter release];

Upvotes: 0

Views: 389

Answers (2)

Mindaugas
Mindaugas

Reputation: 137

Your system time zone is not +0000. So the time is formatted according to it. The solution is to set same time zone to GMT (+0000).

[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Upvotes: 2

werner
werner

Reputation: 859

The reason is you are changing the TimeZone, please comment out the following line and it should give back the time you provided in the dateString:

[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

Upvotes: 0

Related Questions