NSDate being saved as timestamp in CoreData

杀马特。学长 韩版系。学妹 提交于 2019-12-13 12:12:11

问题


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:


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?



来源:https://stackoverflow.com/questions/23861215/nsdate-being-saved-as-timestamp-in-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!