I am currently working on converting the createdAt date in my Nuxtjs application that is fetched from MongoDB using an express app, with the help of moment.js. Initially, when I check if the date is valid, it shows as valid but then it switches to an incorrect fromNow format and turns into an invalid date...
<li class="bookchapterlists__list--items" v-for="(chapter, index) in chap" :key="index">
<nuxt-link
v-if="chapter.index === 0"
class="bookchapterlists__list--items__links"
>Prologue {{chapter.index}}: {{chapter.title}}</nuxt-link>
<nuxt-link
v-else
class="bookchapterlists__list--items__links"
:to="{path: `${ $route.params.id}/${chapter._id}`}"
>
<p
class="bookchapterlists__list--items__links--title"
>Chapter {{chapter.index}}: {{chapter.title}}</p>
<p class="chapter-createdAt">{{chapter.createdAt}}</p>
</nuxt-link>
</li>
created() {
this.$store.commit("book/FORMAT_DATE");
this.chapters.forEach(chapter => {
if (chapter.index % 2 === 1) {
this.rowCount++;
}
});
// console.log(this.rowCount);
},
computed: {
chap: function() {
return this.$store.state.book.book.chapters;
}
}
FORMAT_DATE(state) {
state.book.chapters.forEach((chapter) => {
let createdAt = new Date(chapter.createdAt);
chapter.createdAt = moment(createdAt, 'YYYY-MM-DDYYYY-MM-DDTHH:mm:ss.SSSZ', true).fromNow();
console.log(moment(createdAt, 'YYYY-MM-DDYYYY-MM-DDTHH:mm:ss.SSSZ').isValid())
// console.log(chapter.createdAt);
})
}
The console log statement returns true.