How would you check time difference from two text-boxes in Javascript?
You can set your custom difference by inserting values in end and updatedAt
getDifference(theDate: string): string {
let end = moment(moment(moment()).valueOf());
let updatedAt = moment(new Date(theDate).valueOf());
let diff = end.diff(updatedAt, "hour", false);
if (diff > 8760) {
diff = end.diff(updatedAt, "years", false);
return diff > 1 ? diff + " years ago" : diff + " year ago";
} else if (diff > 730) {
diff = end.diff(updatedAt, "months", false);
return diff > 1 ? diff + " months ago" : diff + " month ago";
} else if (diff > 24) {
diff = end.diff(updatedAt, "days", false);
return diff > 1 ? diff + " days ago" : diff + " day ago";
} else if (diff <= 0) {
diff = end.diff(updatedAt, "minutes", false);
return diff > 1 ? diff + " minutes ago" : diff + " minute ago";
} else return diff > 1 ? diff + " hours ago" : diff + " hour ago";
}