如何在Firestore中保存日期值 [英] How to save a date value in firestore

查看:98
本文介绍了如何在Firestore中保存日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

type="date"在Firestore中另存为字符串.

type="date" in html saves as a string in firestore.

在html文件中.

<input #scheduledStartDate="ngModel" [(ngModel)]="reposition.scheduledStartDate"
name="scheduledStartDate" id="scheduledStartDate" class="input is-normal"
type="date" required placeholder="Leaving...">

从.ts文件界面

scheduledStartDate?: DateTimeFormat;

也尝试过

scheduledStartDate?: Date;

具有相同的结果.

两个选项都将输入的日期值另存为字符串,例如在Firestore中. "2018-01-02",而不是时间戳.

Both options save the inputted date value as a string in firestore e.g. "2018-01-02" instead of a timestamp.

推荐答案

在将javascript对象保存到firestore之前,只需创建Date()的实例并传递类似new Date("December 10, 1815")

Before saving the javascript object to firestore, just create an instance of Date() and pass the value like this new Date("December 10, 1815")

var docData = {
    stringExample: "Hello world!",
    booleanExample: true,
    numberExample: 3.14159265,
    dateExample: new Date("December 10, 1815"),
    arrayExample: [5, true, "hello"],
    nullExample: null,
    objectExample: {
        a: 5,
        b: {
            nested: "foo"
        }
    }
};
db.collection("data").doc("one").set(docData).then(function() {
    console.log("Document successfully written!");
})

Cloud Firestore支持,

日期和时间-按时间顺序-当存储在Cloud Firestore中时,精确到毫秒.四舍五入的精度.

Date and time - Chronological - When stored in Cloud Firestore, precise only to microseconds; any additional precision is rounded down.

注意

当查询涉及具有混合类型值的字段时,Cloud Firestore将基于内部表示使用确定性排序.

Note

When a query involves a field with values of mixed types, Cloud Firestore uses a deterministic ordering based on the internal representations.

https://firebase.google.com/docs/firestore/管理数据/数据类型 https://firebase.google.com/docs/firestore/manage-data/add-data

这篇关于如何在Firestore中保存日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆