問題描述
在不考慮時區的情況下,在 MongoDB 中存儲日期的最佳方式是什么?
Which is the best way to store a date in MongoDB without considering the timezone?
我試著用一個例子來解釋這個問題:
I try to explain the problem with an example:
意大利的Giuseppe在日期December 15, 2014"
創建了一個invoce,當系統存儲這個日期時,MongoDB中將存儲2014-12-14T23:00:00.000Z"
.
Giuseppe in Italy created an invoce in date "December 15, 2014"
, when the system store this date, in MongoDB will be stored "2014-12-14T23:00:00.000Z"
.
John 在英國閱讀了發票并看到 December 14, 2014"
作為發票日期.
John in UK read the invoice and see "December 14, 2014"
as the invoice date.
顯然這個信息是錯誤的,因為 Jhon 也應該看到 "December 15, 2014"
作為發票日期,我該如何解決這個問題?
Obviously this information is wrong becouse also Jhon should see "December 15, 2014"
as the invoice date, how can I solve this problem?
推薦答案
不應將整個日期放在 Date
對象中.盡管有這個名字,Date
代表一個日期和一個時間.如果您正在使用的值表示整個日期,而不是該日期的特定時間,那么您應該以另一種格式存儲該值.
Whole dates should not be placed in a Date
object. Despite the name, Date
represents a date and a time. If the value you're working with represents a whole date, rather than a specific time on that date, then you should store the value in another format.
選項是:
YYYY-MM-DD
或YYYYMMDD
格式的字符串- 自某個參考點以來的整數天數.
這篇關于在 MongoDB 中存儲日期而不考慮時區的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!