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 secondsfunction 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 = "【reserve】" + f_d.toLocaleString(); } else if (ct < 60) { f_time = (ct) + 'Seconds ago'; } else if (ct < 3600) { f_time = (ct / 60) + 'Minutes ago'; } else if (ct < 86400)//one day { f_time = (ct / 3600) + 'Hours ago'; } else if (ct < 604800)//7 days { day = (ct / 86400); if (day < 2) f_time = 'yesterday'; else f_time = day + 'Day Ago'; } else { day = (ct / 86400); f_time = day + 'Day Ago'; } return f_time; } function getd(time1) { var da = new Date(); sda = new Date(time1); var time2 = (); 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(" + () + ":" + () + ")"; } else if (day < 30) { return day + "Day Ago"; } else if (day < 360) { var moth = parseInt(day / 30); return moth + "Monthly ago"; } else { var year = parseInt(day / 360); return year + "New Year's Eve"; }; } 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, Wednesday, Thursday, Friday, and Saturday]","g"); var q = obj_datetine.replace(rg, "").replace("-","/"); //obj_datetine is the time parameter to be processed var d = new Date((q)); return friendly_time(() / 1000);
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.