Right way to convert string into NSDate?(iphone)

旧巷老猫 提交于 2019-12-18 18:24:44

问题


In my app I store the date as a string("MM/dd/YYYY" format). In the DB. later when I retrieve the string I have to compare two dates, how do I achieve this?

If I had used the"YYYY/MM/dd" format I could have directly compared it as strings. Now I have to convert back into NSDate object using "nsdateformatter" and "dateFromString". But I get a constant date value using this no matter what I do.

Any ideas??


回答1:


NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* d = [df dateFromString:@"12/23/2046"];
NSLog(@"%@", d);

Result:

2046-12-23 00:00:00 +0800


来源:https://stackoverflow.com/questions/2162058/right-way-to-convert-string-into-nsdateiphone

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