NSDate被保存为时间戳记在CoreData中 [英] NSDate being saved as timestamp in CoreData

查看:101
本文介绍了NSDate被保存为时间戳记在CoreData中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在CoreData中将日期保存在我的iOS应用程序中.在核心数据中,我的日期属性的类型为日期".在我的NSManagedObjectSubclass文件中,该属性的类型为:NSDate(可以).核心数据中的日期存储为:2014/05/16 14:54:51,当我在SQLite3导航器中检查实际值时,它以以下格式存储:

I currently am saving dates in my iOS application in CoreData. In Core Data, my date attribute is of type "Date". In my NSManagedObjectSubclass file, the attribute is of type: NSDate (which is fine). The date in Core Data is being stored as: 2014/05/16 14:54:51 and when I check the actual values in my SQLite3 navigator, it is being stored in the form:

日期为421959291.293972

421959291.293972 for the date

如何获取上述日期值,是否有办法将其转换回以下格式:2014/05/16 14:54:51?我问这个问题是因为我试图通过CSV文件创建数据集,并希望确保Core Data/SQLite3将我在CSV文件中输入的所有值正确转换为正确的格式.

How do I get the above value for the date, and is there a way to convert it back to the form: 2014/05/16 14:54:51? I'm asking this because I am trying to create a dataset via a CSV file, and want to make sure that all the values that I enter in the CSV file are converted correctly by Core Data/SQLite3 into the right format.

推荐答案

如果使用sqlite3作为命令行工具,请使用datetime函数来解释数据,例如

if you are using sqlite3 as the command line tool, use the the datetime function to interpret the data, so for example

sqlite3> .schema
create table ZENTITY (... ZSTARTDATE TIMESTAMP, ...)
sqlite3> select datetime(zstartdate,'unixepoch','31 years') from ZENTITIY;

这应该给出存储在实体表中的格式化时间戳记

This should give the formatted timestamp stored in the entity table

unix epoch/31 years自变量来自此堆栈溢出问题. 幕后:核心数据日期与31年偏移量?

The unix epoch/31 years arguments come from this stack overflow question. Behind The Scenes: Core Data dates stored with 31 year offset?

这篇关于NSDate被保存为时间戳记在CoreData中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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