user2000452
user2000452

Reputation:

Converting NSString to NSDate - wrong format

Hi I am trying to convert a string into NSDate format - but it returns value with wrong format and also in GMT time.

 NSString *myString = @"10:30 AM";
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 NSTimeZone *local = [NSTimeZone systemTimeZone];
 dateFormatter.dateFormat = @"hh:mm a";
[dateFormatter setTimeZone:local];
 NSDate *myDate = [dateFormatter dateFromString:myString];



NSLog(@"myDate---%@",myDate);

But this returns

  myDate---2000-01-01 05:30:00 +0000

Where I am making the mistake, what to do to get the date as 10:30 AM, Please help me to fix this. Thanks in advance

Upvotes: 1

Views: 150

Answers (1)

Droppy
Droppy

Reputation: 9721

Don't use NSLog() to display the date as that will format it using the UTC/GMT timezone and its own format; instead use the date formatter to display it:

NSLog(@"myDate---%@", [dateFormatter stringFromDate:myDate]);

Upvotes: 3

Related Questions