This article describes the method of JavaScript to implement detailed time reminder information effects. Share it for your reference. The details are as follows:
We often see very humanized time prompts on social networks, such as what your friends updated a few minutes ago, and what information your friends updated a few days ago.
These small tips are much more humane than directly displaying a certain year and a month. We can use different programs to achieve this effect. Below I use the foreground javascript to achieve this effect.
This can reduce the pressure on the backend server.
The javascript implementation code is as follows:
Copy the code as follows:/ This function implements a more humanized time prompt
// @param date_str The time passed, the time format is as follows: 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)+"minute ago";
if(!date_str_w && date_seconddiff <3600)date_str_w= "1 hour 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]+"sign"+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]+"sign"+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]+"sign"+date_arr[3]+':'+date_arr[4];
document.write(date_str_w);
};
I hope this article will be helpful to everyone's JavaScript programming.