Hello, i have some datetimes stored on a database 2018-06-12 04:24:26
, i want to get this datetime in format like xxx minutes/seconds/hours/days ago, here’s what i did:
function to convert datetime:
timeSince(datez) {
var nowdate: any= new Date();
var seconds = Math.floor((nowdate - datez) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " years";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes";
}
return Math.floor(seconds) + " seconds";
}
**function to get data from the database and putting the datetime in the format " xxx minutes ago" **:
public async getNotifs(){
return this.authServiceProvider.getDataz("getNotifications/"+this.myAccountId)
.then(data => {
this.notifData=data;
this.notifData.forEach(function (Data) {
console.log(this.timeSince(Data.datetime));
});
}, (err) => {
console.log(err);
});
}
Here’s my error log:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'timeSince' of undefined
TypeError: Cannot read property 'timeSince' of undefined
at main.js:470
at Array.forEach (<anonymous>)
at main.js:469
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:4982)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:4973)
at main.js:470
at Array.forEach (<anonymous>)
at main.js:469
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:4982)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:4973)
at c (polyfills.js:3)
at polyfills.js:3
at polyfills.js:3
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:4982)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:4973)