This article gives an example of how JavaScript implements a detailed time reminder information effect. Share to everyone for your reference. Specifically as follows:
We often see a very human time cue on social networks, such as what your friend updated a few minutes ago, and what information your friends updated a few days ago.
These tips are a lot more humane than just showing a certain month. We can use different programs to achieve this effect. Here's what I do with the foreground JavaScript.
This can reduce the pressure on the backend server.
The JavaScript implementation code is as follows:
Copy Code code as follows:
This function implements a more humane time hint
@param date_str pass over time, time format such as: 2010-12-14 18:36:09
function Date_parser_diff_return (DATE_STR) {
var date=new date ();
if (typeof (Date_str)!= ' string ') return date;
var date_arr=date_str.split (New RegExp ("[: | | -] "", "IG");
var date_obj = new Date (date_arr[0],date_arr[1]-1,date_arr[2],date_arr[3],date_arr[4],date_arr[5]);
var date_seconddiff= (new Date (). GetTime ()-date_obj.gettime ())/1000;
Date_str_w= ';
if (Date_seconddiff <60*30) date_str_w= Math.ceil (DATE_SECONDDIFF/60) + "Minutes ago";
if (!date_str_w && date_seconddiff <3600) date_str_w= "1 hours ago";
if (!date_str_w && date_seconddiff <3600*2) date_str_w= "2 hours ago";
if (!date_str_w && date_seconddiff <3600*3) date_str_w= "3 hours ago";
if (!date_str_w && date.getfullyear () ==date_arr[0] && date.getmonth () ==date_arr[1]-1 && Date.getdate () ==date_arr[2])
Date_str_w= "Today" +date_arr[3]+ ': ' +date_arr[4];
if (!date_str_w && date.getfullyear () ==date_arr[0] && date.getmonth () ==date_arr[1]-1 && Date.getdate () -1==date_arr[2])
Date_str_w= "Yesterday" +date_arr[3]+ ': ' +date_arr[4];
if (!date_str_w && date.getfullyear () ==date_arr[0] && date.getmonth () ==date_arr[1]-1 && Date.getdate () -2==date_arr[2])
Date_str_w= "The day before Yesterday" +date_arr[3]+ ': ' +date_arr[4];
if (!date_str_w && date.getfullyear () ==date_arr[0] && date.getmonth () ==date_arr[1]-1)
Date_str_w= (Date.getmonth () +1) + "month" + date_arr[2]+ "+date_arr[3]+ ': ' +date_arr[4";
if (!date_str_w && date.getfullyear () ==date_arr[0])
Date_str_w= "This year" + date_arr[1]+ "month" + date_arr[2]+ "No." +date_arr[3]+ ': ' +date_arr[4];
if (!date_str_w && date.getfullyear () -1==date_arr[0])
Date_str_w= "Last year" + date_arr[1]+ "month" + date_arr[2]+ "No." +date_arr[3]+ ': ' +date_arr[4];
document.write (Date_str_w);
};
I hope this article will help you with your JavaScript programming.