【问题标题】:In what date format should I store in the Firestore to create a dynamic report of per month?我应该以什么日期格式存储在 Firestore 中以创建每月的动态报告?
【发布时间】:2021-08-14 07:33:02
【问题描述】:

我有一个项目来存储用户的疫苗接种状态,例如他们的第一剂和第二剂。我想为每种疫苗创建一个动态图表报告。我计划每月和每年创建一个动态报告,我的数据库是 Firestore。

类似于这种图:

由于我将存储用户第 1 剂和第 2 剂的日期,我应该以什么日期格式存储它以确保动态报告。另外,对于图表,我将使用 chart.js

【问题讨论】:

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


    【解决方案1】:

    ISO 字符串既可读又可排序:

    const date = new Date();
    date.toISOString();
    // => "2021-08-14T08:01:29.134Z"
    

    如果您不需要可读性,对于 DB 磁盘空间来说,epoch time 可能更好:

    const date = new Date();
    date.getTime();
    // => 1628928089134
    // OR
    Date.now();
    // => 1628928089134
    

    在这两种情况下,您都可以使用返回的值重新创建日期对象并获取用于过滤和分析的月份和年份。

    const recreatedDate = new Date("2021-08-14T08:01:29.134Z");
    // alternatively:
    // const recreatedDate = new Date(1628928089134);
    
    // January is month number 0
    recreatedDate.getMonth() + 1
    // => 8
    
    console.log(recreatedDate.getFullYear());
    // => 2021
    

    【讨论】:

    • 它是否适用于每月和每年的动态报告?
    • 我看不出为什么不这样做。你有什么顾虑?
    猜你喜欢
    • 2017-01-17
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多