【问题标题】:Storing NSDate with system time zone in Core Data在核心数据中使用系统时区存储 NSDate
【发布时间】:2011-10-14 05:38:44
【问题描述】:

当我在 CoreData 中将日期存储为 [NSDate 日期] 时,它会保存 -5:30 的差异。在核心数据中,我使用属性类型作为日期。

如何用时区存储 NSdate?

更新:这是我正在使用的代码:

存储日期:

database = (DataBase*) [fetchResults objectAtIndex:indexVal]; 
[database setDate:[NSDate date]]; 
NSError error = nil; 
[managedObjectContext save:&error] 

要检索日期:

DataBase *newDataBase = (DataBase) [fetchResults objectAtIndex:i]; 
NSDate *RetrivedDate = [newDataBase Date]; 
NSLog(@"Retrived Date :",RetrivedDate"); 

在存储之前,我会记录它。它显示当前日期和时间。存储后我立即获取日期。但它显示了 1 天的延迟日期..

【问题讨论】:

    标签: iphone core-data nsdate


    【解决方案1】:

    NSDate 没有时区。它将日期存储为自 GMT 参考日期以来的秒数。

    当您使用 NSDateFormatter 格式化显示日期时应用时区。这将默认选择设备时区。

    【讨论】:

    • 但我将日期存储为 [NSDate date] 并将日期检索为 NSDate *dat = [db Date];它显示了一天和一小时的差异。如何解决它。
    • 而且我在存储到 Db 后立即检查了两个日期之间的差异。然后差了一天。请帮忙
    • 如果你有一个具体的例子,你能用代码更新你的问题吗?
    • 这是我的代码 //storing database = (DataBase*) [fetchResults objectAtIndex:indexVal]; [数据库设置日期:[NSDate 日期]]; NSError 错误=零; [managedObjectContext save:&error] //retriving DataBase *newDataBase = (DataBase) [fetchResults objectAtIndex:i]; NSDate *RetrivedDate = [newDataBase 日期]; NSLog(@"Retrived Date :",RetrivedDate"); 这里 Retrived 日期给出了 1 天 1 小时 15 分钟的差异。
    • 我做到了。在我登录之前存储。它显示当前日期和时间。存储后我立即获取日期。但它显示 1 天延迟日期.. 无论如何感谢您的 cmets
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多