This article has shared the function code for JS displaying friendly time for your reference. The specific content is as follows
//Friendly time return function (such as: 10 minutes ago) time_stamp is UNIX seconds function friendly_time(time_stamp){ var now_d = new Date(); var now_time = now_d.getTime() / 1000; //Get the number of seconds of the current time var f_d = new Date(); f_d.setTime(time_stamp * 1000); var f_time = f_d.toLocaleDateString(); var ct = now_time - time_stamp; var day = 0; if (ct < 0) { f_time = "【Appointment】" + f_d.toLocaleString(); } else if (ct < 60) { f_time = Math.floor(ct) + 'seconds ago'; } else if (ct < 3600) { f_time = Math.floor(ct / 60) + 'minutes ago'; } else if (ct < 86400)//day{ f_time = Math.floor(ct / 3600) + 'hours ago'; } else if (ct < 604800)//7 days{ day = Math.floor(ct / 86400); if (day < 2) f_time = 'yesterday'; else f_time = day + 'day ago'; } else { day = Math.floor(ct / 86400); f_time = day + 'day ago'; } return f_time;}function getd(time1){ var da = new Date(); sda = new Date(time1); var time2 = da.getTime(); var time = 0; if (time1 > time2) { time = time1 - time2; sda = da; } else { time = time2 - time1; } if (time < 1000) return "just"; time = parseInt(time / 1000); if (time > 86400) { var day = parseInt(time / (24 * 60 * 60)); if (day == 1) { return "Yesterday(" + sda.getHours() + ":" + sda.getMinutes() + ")"; } else if (day < 30) { return day + "day ago"; } else if (day < 360) { var moth = parseInt(day / 30); return moth + "month ago"; } else { var year = parseInt(day / 360); return year + "year ago"; }; } else if (time > 3600) { var hour = parseInt(time / (60 * 60)); return hour + "hour ago"; } else if (time > 60) { var hour = parseInt(time / 60); return hour + "minutes ago"; } else { return time + "seconds ago"; }}Sometimes the win7 system will automatically display the time in a format with week, and the time format needs to be processed.
var rg = new RegExp("[Monday,Tuesday,Thai,Friday,Five,66]","g");var q = obj_datetine.replace(rg, "").replace("-","/");//obj_datetine is the time parameter to be processed var d = new Date(Date.parse(q)); return friendly_time(d.getTime() / 1000);The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.