1. Time Stamp Display
Often can see the forum or the news, the time stamp for just, xx minutes ago, xx hours ago and so on, the front-end how to achieve it?
2. Time Stamp Implementation
This function is relatively simple, directly on the function, TIME_STR is the timestamp of the string result, converted to a Date object,
The Date object can get information such as month and date, new a date represents the current time, and after the relevant information is compared, the custom returns the result.
Time processing function, how many minutes ago, how many hours ago, more than 24 hours to display the date, more than one year display month day
Gettimeshow:function (TIME_STR) {
Debugger
var now = new Date ();
var date = new Date (TIME_STR);
Calculate the time interval, measured in minutes
var inter = parseint ((Now.gettime ()-Date.gettime ())/1000/60);
if (inter = = 0) {
return "just";
}
How many minutes ago
else if (Inter < 60) {
return inter.tostring () + "Minutes ago";
}
How many hours ago?
else if (Inter < 60*24) {
return parseint (INTER/60). toString () + "hour Ago";
}
This year, dates are different, date + time format 06-13 22:11
else if (now.getfullyear () = = Date.getfullyear ()) {
Return (Date.getmonth () +1). ToString () + "-" +
Date.getdate (). toString () + "" +
Date.gethours () + ":" +
Date.getminutes ();
}
else{
Return Date.getfullyear (). toString (). SUBSTRING (2, 3) + "-" +
(Date.getmonth () +1). ToString () + "-" +
Date.getdate (). toString () + "" +
Date.gethours () + ":" +
Date.getminutes ();
}
},
3. Realize the effect
"Web front end" JS rendering timestamp is compared with the current time results