JSFiddle - React, Tailwind, and code Playground
CSS
if(getYear <=0) {
var postYear='';
}
if(getMonth <=0) {
var postMonth='';
}
if(getDay <=0) {
var postDay='';
}
else {
var postDay=getDay + ' days';
}
if(getHours <=0) {
var postHour='';
}
else {
var postHour=getHours + ' hours';
}
if(getMinutes <=0) {
var postMinute='';
}
else {
var postMinute=getMinutes + ' minutes';
}
if(getSeconds <=0) {
var postSecond='';
}
else {
var postSecond=getSeconds + ' seconds';
}
JavaScript
var NOW = new Date();
var Time = new Date(1510482225318);
var dateDiff = function(data1, date2) {
var diff = Math.abs(data1 - date2);
if (Math.floor(diff / 31536000000)) {
console.log(Math.floor(diff / 31536000000) + " year");
} else if (Math.floor(diff / 86400000)) {
console.log(Math.floor(diff / 2592000000) + " months");
} else if (Math.floor(diff / 86400000)) {
console.log(Math.floor(diff / 86400000) + " days");
} else if (Math.floor(diff / 3600000)) {
console.log(Math.floor(diff / 3600000) + " hours");
} else if (Math.floor(diff / 60000)) {
console.log(Math.floor(diff / 60000) + " minutes");
} else {
console.log("now");
}
}
dateDiff(NOW, Time);