I didn't get correct time when using time formatter

我只是一个虾纸丫 提交于 2019-12-13 09:05:23

问题


When i tried to get date and time from two textfields, i got incorrect time. my input

date.text = may 20 2000    
time.text = 02:00 AM

but i got the time in nslog is :00:00:00 





NSString *datestring = self.date.text;
NSString *timestring = self.time.text;
NSString *combined = [NSString stringWithFormat:@"%@ %@", datestring, timestring];
NSLog(@"%@",combined);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM dd yyyy HH:mm:ss"];
NSDate *dates = [dateFormat dateFromString:combined];
self.create.hidden = YES;
NSLog(@"%@",dates);

any help please?


回答1:


You have time.text = 02:00 AM and you set your formatter on HH:mm:ss.

It's a wrong formatter, that's why is null.




回答2:


You have wrong formatter, it must be:

[dateFormat setDateFormat:@"MMMM dd yyyy hh:mm aa"];


来源:https://stackoverflow.com/questions/23891266/i-didnt-get-correct-time-when-using-time-formatter

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