Hi I want to convert the mongo db created_at timestamp to when It says ... minutes/hours ago using the date-fns library. The function is called formatDistanceToNow
which returns time since the date time provided. I'm using Vue for front end but can't seem to get it working.
<template>
<div class="feed">
<div v-for="post in feed" :key="post.id" class="post">
<h3>{{ post.name }}</h3>
<p>{{ post.timestamp }}</p> // return 2021-06-12T12:59:57.337Z
<p>{{ Date(post.timestamp) }}</p> // return Mon Jun 14 2021 16:02:22 GMT+0100 (British Summer Time)
<!-- <p>{{ formatDate(post.timestamp) }}</p> -->
<!-- <p>{{ formatDate(Date(post.timestamp)) }}</p> -->
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
import { formatDistanceToNow } from 'date-fns'
export default {
computed: {
...mapState(['feed']),
formatDate(timestamp){
return formatDistanceToNow(timestamp)
}
}
}
</script>
The 2 commented lines of code is what I've tried, but keep getting the following error
Uncaught (in promise) RangeError: Invalid time value