代码之家  ›  专栏  ›  技术社区  ›  Govind Samrow

如何将数据保存为Firestore Timestamp对象

  •  0
  • Govind Samrow  · 技术社区  · 7 年前

    只想在Firestore数据库中将日期保存为时间戳,但它将时间戳存储为数字:

    save() {
        const data = this.transactionForm.value;
        data.type = data.type == 'true' || data.type == true ? true : false;
        data.updated = firebase.firestore.FieldValue.serverTimestamp();
        // Date string to timestamp
        data.date =  new Date(data.date.split("-").reverse().join("-")).getTime();
        let id = data.id;
        this.transCollection.doc(id).update(data);
    }  
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Martin Lund Andreas Engedal    7 年前
      unixToTimestamp() {
        const date = new Date();
        const hours = date.getHours();
        const minutes = date.getMinutes();
        const seconds =  date.getSeconds();
        const day = date.getDate();
        const month = date.getMonth() + 1;
        const year = date.getFullYear();
    
        console.log(hours + ':' + minutes + ':' + seconds);
        console.log(day + '/' + month + '/' + year);
      }
    

    11:25:07
    19/11/2018
    

    通过这种方式,您可以选择如何创建时间戳并将其保存在firestore中

    你也可以按你想要的方式打印月份。

    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    
    const month = months[date.getMonth()];
    

    这是因为日期.getMonth()得到索引,这就是为什么我用+1得到一个月。

        2
  •  0
  •   taiwo sunday    7 年前

    long

    推荐文章