如何使用 vue Js 将 firestore TIMESTAMPS 转换为完整日期

fol*_*e83 3 javascript firebase vue.js

我想将我从 Google firestore 获得的时间戳转换为我的 vueApp 中的天数和月数。

我得到一个这样的对象 Object { seconds: 1549843200, nanoseconds: 0 }

<template>
   <div v-for="note in notes" :key="note.id" class="note">
     <div class="note_dates">
       <span class="day">{{note.day}}</span>
       <span class="month">{{note.month}}</span>
       .........
</template>

<script>
export default {
  data() {
    return {
      notes: []
    };
  },

  methods: {},
  created() {
    notesCollection.get().then(querySnapshot => {
      querySnapshot.forEach(doc => {
        const query = {
          id: doc.id,
          content: doc.data().content,
          day: doc.data().created_on,
          month: doc.data().created_on
        };

        this.notes.push(query);
      });
    });
  }
Run Code Online (Sandbox Code Playgroud)

该值created_on是 firestore 集合中的时间戳

因此,在我的视图组件中,我只想回显时间戳中的日期和月份。

Dan*_*eck 8

seconds从时间戳中取出值,乘以 1000 得到 ms 值,然后使用Date()

  let timestamp = { seconds: 1549843200, nanoseconds: 0 } // firebase data     
  let myDate = new Date(timestamp.seconds * 1000) // date object
  
  console.log(myDate)
Run Code Online (Sandbox Code Playgroud)

从那里,您可以提取月/日/年或以其他方式完全像任何其他 javascript Date object 一样格式化日期