Converting NSString to NSDate - wrong format

天大地大妈咪最大 提交于 2019-12-04 02:13:07

问题


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


回答1:


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]);


来源:https://stackoverflow.com/questions/25118865/converting-nsstring-to-nsdate-wrong-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!