最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to convert firestore TIMESTAMPS to full dates with vue Js - Stack Overflow

programmeradmin1浏览0评论

I want to convert the timestamp I get from the Google firestore to days and months in my vueApp.

I get an object like this 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);
      });
    });
  }

the value created_on is a timestamp in the firestore collection

So, in my view component, I want to echo just the day and the month from the timestamp.

I want to convert the timestamp I get from the Google firestore to days and months in my vueApp.

I get an object like this 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);
      });
    });
  }

the value created_on is a timestamp in the firestore collection

So, in my view component, I want to echo just the day and the month from the timestamp.

Share Improve this question edited Feb 13, 2019 at 22:36 Pingolin 3,4177 gold badges27 silver badges43 bronze badges asked Feb 13, 2019 at 21:59 foliwe83foliwe83 5901 gold badge11 silver badges26 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

Take the seconds value from the timestamp, multiply by 1000 to get the ms value, and use Date():

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

From there, you can extract the month/day/year or otherwise format the date exactly as you would any other javascript Date object.

I don't think you'll need any external libraries for your use case. Firestore timestamps have a method that returns a date object, so what you could do is:

  1. convert timestamp to date object:

     const query = {
      id: doc.id,
      content: doc.data().content,
      date: doc.data().created_on.toDate()
    };
    

https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp#todate

  1. Use built-in Date methods to get the numerical day and month:

{{ date.getDate() }} and {{ date.getMonth() + 1 }}

Months start counting from 0, so december is 11

If you want to format the date differently, I'd first try if you can use Intl.DateTimeFormat. If that doesn't offer what you need I'd pick something from date-fns that meets your needs.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

https://date-fns.org/

Just to add to Daniel's answer above...there are several date libraries out there...I find momentjs very useful.

Example:

import moment from "moment"

let myDate = created_on.seconds * 1000 // Unix ms timestamp

let myDay = moment(myDate, 'x').format('dddd') // Monday
let myMonth = moment(myDate, 'x').format('MMM') // January

Full Documentation here

发布评论

评论列表(0)

  1. 暂无评论