2015-11-19 83 views
-1

我有一个的NSString其中包含下列格式日期:二零一五年十一月十九日上午10时44分错误的NSString转换成NSDate的

现在我想的NSString转换成NSDate的使用下面的代码块:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"MMM dd,yyyy HH:mm a "]; 
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 
NSDate *setEditDate = [dateFormat dateFromString:lastLog]; 
NSLog(@"setEditDate = %@",setEditDate); 

上面的代码返回我 “setEditDate = 2015年11月19日○点44分00秒+0000”

这里时间得到改变,以10:44 00:44。

如何解决此问题,以获取正确的日期和时间。 任何帮助将是可观的。

+0

它与时区有关吗? –

+0

是的。 Beacuse如果我删除时区formater,我得到以下结果 setEditDate = 2015-11-18 19:14:00 +0000 从指定日期减少1天 – user5247057

+1

更改'@“MMM dd,yyyy HH:mm a”'to '@“MMM dd,yyyy hh:mm a”' – Rajat

回答

-1

你的格式字符串有点不对。而不是HH的时间,你想要hh

0

这里是我使用的代码。代码已经在工作。

NSDate *currentDate = [[NSDate alloc] init]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter setDateFormat:@"MMM dd,yyyy hh:mm a "]; 
NSString *localDateString = [dateFormatter stringFromDate:currentDate]; 
NSLog(@"localDateString : %@",localDateString); 

我希望它有帮助。