JS display time difference a few minutes ago, a few hours ago, a few weeks ago.

Source: Internet
Author: User

Window.onload = function () {
var show_times = $ (". Times span");
for (Var i=0;i<show_times.length;i++) {
var datestr = show_times[i].innerhtml;
var datetimestamp = Date.parse (Datestr.replace (/-/gi, "/"));
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var month = day * 30;
Get current time
var now = new Date (). GetTime ();
var diffvalue = Now-datetimestamp;
var monthc =diffvalue/month;
var weekc =diffvalue/(7*day);
var dayc =diffvalue/day;
var hourc =diffvalue/hour;
var MinC =diffvalue/minute;
if (monthc>=1) {
show_times[i].innerhtml = Datestr;
}
else if (weekc>=1) {
result= "Posted in" + parseint (WEEKC) + "weeks ago";
show_times[i].innerhtml = result;
}
else if (dayc>=1) {
result= "Posted in" + parseint (DAYC) + "days ago";
show_times[i].innerhtml = result;
}
else if (hourc>=1) {
result= "Posted in" + parseint (HOURC) + "hours ago";
show_times[i].innerhtml = result;
}
else if (minc>=1) {
result= "Posted in" + parseint (MinC) + "Minutes ago";
show_times[i].innerhtml = result;
}else{
Result= "published in 1 minutes";
show_times[i].innerhtml = result;
}
}
}

JS display time difference a few minutes ago, a few hours ago, a few weeks ago.

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.