gpt4 book ai didi

objective-c - NSDateFormatter 生成(空)SQLite 和 iOS 5.1

转载 作者:行者123 更新时间:2023-11-30 12:48:53 27 4
gpt4 key购买 nike

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

一些背景:

这个 iPad 应用程序将在不同的国家/地区使用,我需要计算日期,看看自用户上次登录以来是否已经过去了 90 天。

我有一个 SQLite 数据库,其中 DateLastIn 字段设置为 TEXT

我的对象有一个 DateLastIn 属性设置为 NSDate

当填充我的记录对象时,我设置了一个 NSDateFormatter 。

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

然后我读入 DateLastIn(使用 SQLite 的 FMDB 包装器)。

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

结果:DateLastIn 直接来自数据库(字符串)显示 2012-04-23 18:20:51

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

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

结果:对字符串日期使用格式化程序会导致:(null)

我在 SQLite 中尝试过 DATETIME,我尝试在我的对象中使用 NSString 而不是 NSDate,并且似乎在兜圈子。

非常感谢任何帮助。

最佳答案

NSDateFormatter 使用 format patterns from the Unicode Technical Standard #35 .

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

关于objective-c - NSDateFormatter 生成(空)SQLite 和 iOS 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41262879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com