This article mainly introduces how to implement detailed time reminder information in javascript, and involves the skills of javascript operation time, which has some reference value, for more information about how to implement detailed time reminder in javascript, see the example in this article. Share it with you for your reference. The details are as follows:
We often see user-friendly 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 shows a lot of human nature in a certain month. We can use different programs to achieve this effect. Below I will use the front-end javascript to achieve this effect.
This reduces the pressure on backend servers.
The javascript implementation code is as follows:
The Code is as follows:
// This function provides a more user-friendly time prompt
// @ Param date_str: the time passed by param date_str. The time format is as follows: 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 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] + "Number" + 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 design javascript programs.