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

查看:17
本文介绍了NSDateFormatter 生成(空)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.

一些背景:

此 iPad 应用将在不同国家/地区使用,我需要计算日期以查看自用户上次登录后是否已过去 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(使用 SQLite 的 FMDB 包装器).

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 结果:(空)

我在 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 中的格式模式.

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

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

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

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