【问题标题】:How to get date and time from firebase.firestore.FieldValue.serverTimestamp() [duplicate]如何从 firebase.firestore.FieldValue.serverTimestamp() 获取日期和时间 [重复]
【发布时间】:2021-07-30 16:00:05
【问题描述】:

当我在终端上控制台记录我的创建时,它会显示

“创造”:对象{ “纳秒”:420000000, “秒”:1620290402, },

如何将其格式化为日期和时间,以便在前端使用它

【问题讨论】:

标签: javascript firebase react-native google-cloud-firestore timestamp


【解决方案1】:

这是我过去用于 Firebase 到 JS 字符串的一种快速格式:

    //Firebase Timestamp Fix

    creation = new Date(creation.seconds * 1000);
    const formattedDate = creation.toLocaleDateString("en-US");
    const formattedTime = creation.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true });
    const newDate = `${formattedDate} ${formattedTime}`;

【讨论】:

    【解决方案2】:

    javascript 对 firebase 时间戳一无所知。它只是为您提供对象

    所以只需使用 toDate()。 如需进一步参考,请查看此来源toDate()firestore

    【讨论】:

      猜你喜欢
      • 2018-12-27
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      相关资源
      最近更新 更多