iOS-CoreData 中date对象数据在数据库的显示

前提条件:使用CoreData
问题出现:在iOS开发过程中,假如使用到CoreData,保存时间的对象又用到date对象,当我们查看数据库的时候,发现date对应的秒数不是时间戳,怎么转换也转换不了正确的时间。

原因:Core Data的date对象存的时间对象是使用timeIntervalSinceReferenceDate (相对与2001年1月1日)的时间对象
解决方法:计算timeIntervalSince1970 与1970年1月1日相差的秒数
通过计算得出:timeIntervalSince1970和timeIntervalSinceReferenceDate 相差的秒数是978307200
那么时间戳:coreData数据库时间秒数+978307200

举例:
假如,coreData数据库时间是:628747200,时间戳:1607054400, 转换后时间就是:2020-12-04 12:00:00
在这里插入图片描述

参考excel时间转换公式:
https://github.com/samuelandkevin/CoreData_Date

Guess you like

Origin blog.csdn.net/samuelandkevin/article/details/110734593