【问题标题】:How to get year and month from firebase?如何从firebase获取年份和月份?
【发布时间】:2020-10-14 11:57:09
【问题描述】:

我在 firebase“cloud firestore”中有这个字段 firebase date field

在我想提取的云函数中(仅从该日期获取年份,仅获取年份和月份)

  const year = BookedSessionChanges.bookedAt.toDate();
    console.log("yearr ", year);
    const month = BookedSessionChanges.bookedAt.toDate();
    console.log("month ", month);

这是云函数中的代码,我使用 toDate() 将上图中显示的日期转换为旁边的 2020-10-06T08:38:14.066Z 时间。

那么我怎样才能从这个日期只得到年份和月份呢? Ps. 我尝试了 getYear() 和 toDate("Y") ...但这些都不起作用。

有什么帮助吗?谢谢

【问题讨论】:

    标签: javascript typescript firebase date google-cloud-firestore


    【解决方案1】:

    toDate() 方法将 Firestore Timestamp 对象转换为 Javascript Date 对象。 转换后可以使用getYear()方法提取年份。

    const year = BookedSessionChanges.bookedAt.toDate().getYear();
    

    【讨论】:

    • 现在我检查了它,发现我应该使用 getFullYear() 而不是 getYear() 因为年份高于 2000 并且它有效。谢谢,我会在下面添加我的答案。
    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2013-06-23
    相关资源
    最近更新 更多