将字符串转换为NSDate的正确方法?(iphone) [英] Right way to convert string into NSDate?(iphone)

查看:126
本文介绍了将字符串转换为NSDate的正确方法?(iphone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将日期存储为字符串(MM / dd / YYYY格式)。在DB中。以后当我检索字符串时我必须比较两个日期,我该如何实现呢?

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?

如果我使用了YYYY / MM / dd格式我可以拥有直接将其比作字符串。
现在我必须使用nsdateformatter和dateFromString转换回NSDate对象。但无论我做什么,我都会得到一个恒定的日期值。

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.

任何想法?

推荐答案

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

结果:

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

这篇关于将字符串转换为NSDate的正确方法?(iphone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆