NSDateFormatter生成(null)SQLite和iOS 5.1 [英] NSDateFormatter producing (null) SQLite and iOS 5.1

查看:107
本文介绍了NSDateFormatter生成(null)SQLite和iOS 5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人可以帮助解释为什么在将字符串转换为日期时获得空值。这一切都看起来不错,但我显然在这里遗漏了一些东西。

Maybe somebody can help explain why I am getting a null value when converting a string to a date. It all looks right but I'm obviously missing something here.

某些背景:

在不同的国家使用,我将需要在日期进行计算,看看用户上次登录后是否已经过了90天。

This iPad app will be used in different countries and I will need to do a calculation on the date to see if 90 days have passed since a user last logged in.

我有一个SQLite数据库将DateLastIn字段设置为TEXT

I have a SQLite Database with a DateLastIn field set as TEXT

我的对象的DateLastIn属性设置为NSDate

My Object has a DateLastIn property set as NSDate

填充我的记录时对象我设置一个NSDateFormatter这样..

When populating my record object I set up a NSDateFormatter as such..

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];  // SQLite default date format

然后我读取DateLastIn(使用FMDB封装为SQLite) / p>

Then I read in the DateLastIn (Using FMDB wrapper for SQLite).

        // test SQLite as String
        NSString *testDate = [results stringForColumn:@"DateLastIn"];
        NSLog(@"DateLastIn straight from DB (string) shows %@", testDate);

结果:
DateLastIn直接从DB(字符串)显示2012-04- 23 18:20:51

到目前为止,一切都很好。接下来,我测试将其转换为NSDate对象,例如

All is good so far. Next I test converting this to an NSDate object e.g

        NSDate *aDate = [[NSDate alloc] init];
        aDate = [formatter dateFromString:testDate];
        NSLog(@"Using formmater on string date results in: %@", aDate);

结果:
在字符串日期使用formmater会导致:(null)

我已经在SQLite中尝试过DATETIME,我已经尝试在我的对象中使用NSString,而不是NSDate,并且似乎在循环中。

I have tried DATETIME in SQLite, I've tried using NSString in my object instead of NSDate and seem to be going around in circles.

任何帮助不胜感激。

推荐答案

NSDateFormatter 使用Unicode技术标准中的格式模式# 35

NSDateFormatter uses the format patterns from the Unicode Technical Standard #35.

对于小时格式,您需要 HH (小时[0-23])不是 hh (小时[1-12])。

For the hour format, you need HH (Hour [0-23]) not hh (Hour [1-12]).

这篇关于NSDateFormatter生成(null)SQLite和iOS 5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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