"Web front end" JS rendering timestamp is compared with the current time results

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.