将日期转换为时间戳以在 javascript 中存储到 firebase firestore
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53482750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert date to timestamp for storing into firebase firestore in javascript
提问by Arjen de Jong
I'm currently using Math.floor(Date.now() / 1000)to get the correct timestamp format to add into a document in Firebase, however, the timestamp gets inserted as a number, and not as a timestamp.
我目前正在使用Math.floor(Date.now() / 1000)获取正确的时间戳格式以添加到 Firebase 中的文档中,但是,时间戳被插入为数字,而不是时间戳。
I would like to have it inserted as shown below (as a timestamp, not as a number).
我想按如下所示插入它(作为时间戳,而不是数字)。
Is this possible?
这可能吗?
回答by Sean Stayns
You can simply use the following line:
您可以简单地使用以下行:
var myFirebaseFirestoreTimestampFromDate = firebase.firestore.Timestamp.fromDate(new Date());
.fromDateis a static methodfrom the static Timestamp classfrom Firebase.
.fromDate是来自Firebase的静态 Timestamp 类的静态方法。
回答by Doug Stevenson
If you want to store a field as a timestamp in Firestore, you'll have to send a JavaScript Dateobject or a Firestore Timestampobject as the value of the field.
如果要将字段存储为 Firestore 中的时间戳,则必须发送 JavaScript Date对象或 Firestore Timestamp对象作为该字段的值。
If you want to use Date, simply say new Date(x)where xis the value in milliseconds that you were trying to add before.
如果你想使用日期,只是说new Date(x)这里x是您试图之前添加以毫秒为单位的值。
If you want to use Timestamp, you would have to do more work to get that xconverted into a combination of seconds and nanoseconds to pass to the constructor, as the documentation suggests.
如果您想使用时间戳,则必须做更多的工作才能将其x转换为秒和纳秒的组合以传递给构造函数,如文档所示。
回答by Hyman Woodward
As i can see you are doing date added you would be better using the Server Timestamp and using the below security rule to enforce it.
正如我所看到的,您正在添加日期,您最好使用服务器时间戳并使用以下安全规则来强制执行它。
Security Rules
安全规则
allow create: if request.resource.data.date_added == request.time &&
// other rules for the message body
Client side JS code
客户端JS代码
const message = {
date_added: firebase.firestore.FieldValue.serverTimestamp();
}
回答by JakeSteam
To store a date / timestamp in Firestore, you need to send a Dateobject.
要在 Firestore 中存储日期/时间戳,您需要发送一个Date对象。
For example, with an existing date, set a field to: new Date("December 10, 1815")
例如,对于现有日期,将字段设置为: new Date("December 10, 1815")
Further information is available in the docs.
回答by Hasintha Abeykoon
I solved it by using:
我通过使用解决了它:
const created = firebase.firestore.Timestamp.fromDate(new Date()).toDate());
You have to import firebase from 'firebase/app'explicitly in your code.
您必须firebase from 'firebase/app'在代码中显式导入。
import * as firebase from 'firebase/app'
Make sure your createdor whatever constant is in type Datebefore actually being saved to firestore.
在实际保存到 firestore 之前,请确保您的created或任何常量的类型Date。
回答by Love to Code
Late to the game here, but as I read the OP's question it specifically illustrates retaining the timezone offset. Since a timestamp lacks the offset, as the question was asked by the OP it looks like the honest answer is NO.
这里的游戏晚了,但是当我阅读 OP 的问题时,它特别说明了保留时区偏移量。由于时间戳缺少偏移量,因此正如 OP 所提出的问题,看起来诚实的答案是否定的。
I have a related situation right now where I need a representation of a specific 'day' but I need the offset. A timestamp with 0s for h/m/s wouldbe ideal except for the lack of offset. I am forced to either add a second field to store the offset, or to do something like a string:
我现在有一个相关的情况,我需要一个特定“天”的表示,但我需要偏移量。除了缺少偏移外,h/m/s 为 0 的时间戳将是理想的。我被迫添加第二个字段来存储偏移量,或者执行类似字符串的操作:
'20200125-360'
'20200125-360'
回答by HakanC
Use
用
let lang = 'en-US' // you may use user's computer language: navigator.language || navigator.userLanguage
let d = new Date(date);
let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour:"numeric", timeZone:'short' };
firebase.database().ref('/thePathYouLikeToSave/date_added').set(d.toLocaleDateString(lang, options));
for more options of the date here the full details
有关日期的更多选项,请查看完整的详细信息
回答by Akbar Khan
Swift 4 & 5
斯威夫特 4 & 5
Conversion of date in timestamp
时间戳中日期的转换
let timestampFromDate = Timestamp(date: Date())
print(timestampFromDate)


