如何在 sqlite3 数据库中保存和检索日期和时间?

发布于 2024-08-09 11:00:06 字数 302 浏览 2 评论 0原文

我想将当前日期+时间保存到数据库中并也想检索它。我知道在 Objective-C 中我们不能直接保存日期+时间。 我将 double 作为数据库中的字段,目前我通过将其转换为 double 来保存日期。

这是将日期转换为双精度的方法 -

currentDate=[NSDate date];

startDate=(double)[currentDate timeIntervalSince1970];

现在我如何从该双值中检索日期?或者有其他方法可以在 iPhone SDK 中保存日期。

i want to save current date+time into database and also want to retrieve it. i know that in objective-C we cannot save date+time directly.
i have double as a field into database and currently I'm saving date by converting it into double.

here is the method for converting date into double-

currentDate=[NSDate date];

startDate=(double)[currentDate timeIntervalSince1970];

now how do i retrieve date back from that double value? or is there another way to save dates in iPhone SDK.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-08-16 11:00:07
NSDate *dateFromDouble(double interval) { 
  return [NSDate dateWithTimeIntervalSince1970: interval];
}
NSDate *dateFromDouble(double interval) { 
  return [NSDate dateWithTimeIntervalSince1970: interval];
}
西瑶 2024-08-16 11:00:07

另一种选择是将日期转换为 ISO 8601 并将其另存为 TEXT;这可以使用 -description 和 -initWithString: 来完成。它会使数据库中的日期稍微变大;但出于调试目的,它也更具可读性。

Another alternative is to convert the date to ISO 8601 and save it as TEXT; this can be done using -description and -initWithString:. It will make the date slightly larger in the database; but it'll also be a lot more readable for debugging purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文