# The time stamp shows how many minutes ago and how many days ago the JS was processed

2022-05-15 07:30:59

``````/*
**  The timestamp shows how many minutes ago , How many days ago
** eg.
** console.log(dateDiff(1411111111111));  // 2014 year 09 month 19 Japan
** console.log(dateDiff(1481111111111));  // 9 Month ago,
** console.log(dateDiff(1499911111111));  // 2 Month ago,
** console.log(dateDiff(1503211111111));  // 3 Zhou Qian
** console.log(dateDiff(1505283100802));  // 1 Minutes ago
*/
var dateDiff = function (timestamp) {
//  Complete as 13 position
var arrTimestamp = (timestamp + '').split('');
for (var start = 0; start < 13; start++) {
if (!arrTimestamp[start]) {
arrTimestamp[start] = '0';
}
}
timestamp = arrTimestamp.join('') * 1;

var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime();
var diffValue = now - timestamp;

//  If the local time is less than the variable time
if (diffValue < 0) {
return ' not long ago ';
}

//  Calculate the magnitude of the difference time
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;

//  Numerical complement 0 Method
var zero = function (value) {
if (value < 10) {
return '0' + value;
}
return value;
};

//  Use
if (monthC > 12) {
//  exceed 1 year , Directly display year, month and day
return (function () {
var date = new Date(timestamp);
return date.getFullYear() + ' year ' + zero(date.getMonth() + 1) + ' month ' + zero(date.getDate()) + ' Japan ';
})();
} else if (monthC >= 1) {
return parseInt(monthC) + " Month ago, ";
} else if (weekC >= 1) {
return parseInt(weekC) + " Zhou Qian ";
} else if (dayC >= 1) {
return parseInt(dayC) + " Days ago, ";
} else if (hourC >= 1) {
return parseInt(hourC) + " Hours before ";
} else if (minC >= 1) {
return parseInt(minC) + " Minutes ago ";
}
return ' just ';
};
``````