Asked By: Anonymous
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.
Solution
Answered By: Anonymous
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 _x000D_
let myDate = new Date(timestamp.seconds * 1000) // date object_x000D_
_x000D_
console.log(myDate)
_x000D_
_x000D_
x000D
From there, you can extract the month/day/year or otherwise format the date exactly as you would any other javascript Date object.