Convert eloquant's default timestamps to human readable format
I am developing app with vue spa and laravel api and I need to convert timestamps to human readable format.I have used diffForHumans() before, and I want to get similar result, but on spa side. Is there a way in js to convert a string like this 2022-07-20T10:46:12.000000Z to something like 15 minutes ago
@sinnbeck that would work, but I am doing notifications popup where user sees his notifications, and time difference. So I think in my case fetching notifications whenever user clicks on that button wouldn't be good for performance. however I came up with my own function which did the trick.
timeDiff() {
let laravelTime = new Date(this.notification.created_at);
let nowTime = new Date();
var diff = Math.abs(nowTime - laravelTime);
var minutes = Math.floor(diff / 1000 / 60);
if (minutes >= 60 && minutes <= 1440) {
let time = Math.floor(minutes / 60);
if (time === 1) {
return time + " hour ago";
}
return time + " hours ago";
}
if (minutes < 60) {
let time = Math.floor(minutes);
if (time === 1) {
return time + " minute ago";
}
return time + " minutes ago";
}
let time = Math.floor(minutes / 1440);
if (time === 1) {
return time + " day ago";
}
return time + " days ago";
}