NSDate being saved as timestamp in CoreData

2019-04-14 05:21发布

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 for the date

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.

1条回答
手持菜刀,她持情操
2楼-- · 2019-04-14 05:40

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

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

查看更多
登录 后发表回答