JS display friendly time

Source: Internet
Author: User
// Friendly time return function (for example, 10 minutes ago) time_stamp is UNIX second function friendly_time (time_stamp) {var now_d = new date (); var now_time = now_d.gettime () /1000; // obtain the current time in seconds var f_d = new date (); f_d.settime (time_stamp * 1000); var f_time = f_d.tolocaledatestring (); var Ct = now_time-time_stamp; vaR day = 0; If (CT <0) {f_time = "[reservation]" + f_d.tolocalestring ();} else if (CT <60) {f_time = math. floor (CT) + 'second before ';} else if (CT <3600) {f_time = math. floor (CT/60) + 'minute ago ';} else if (CT <86400) // One day {f_time = math. floor (CT/3600) + 'hour before ';} else if (CT <604800) // 7 days {day = math. floor (CT/86400); If (day <2) f_time = 'Yesterday '; else f_time = day + 'day before';} else {day = math. floor (CT/86400); f_time = day + 'day';} return f_time;} function getd (time1) {var da = new date (); SDA = new date (time1); var time2 = da. gettime (); var time = 0; If (time1> time2) {time = time1-time2; SDA = da;} else {time = time2-time1 ;} if (time <1000) Return "just"; time = parseint (Time/1000); If (Time> 86400) {var day = parseint (Time/(24*60*60); If (Day = 1) {return "yesterday (" + SDA. gethours () + ":" + SDA. getminutes () + ")" ;}else if (day <30) {return day + "Days Ago" ;}else if (day <360) {var moth = parseint (day/30); Return moth + "Months Ago";} else {var year = parseint (day/360 ); return year + "years ago" ;};}else if (Time> 3600) {var hour = parseint (Time/(60*60 )); return hour + "Hours Ago";} else if (Time> 60) {var hour = parseint (Time/60); Return hour + "Minutes Ago ";} else {return time + "seconds ago ";}}

Sometimes the win7 system automatically displays the time in the format of a week.

VaR Rg = new Regexp ("[October 6, 1234]", "G ");
VaR q = obj_datetine.replace (RG, ""). Replace ("-", "/"); // obj_datetine is the time parameter to be processed
var d = new Date(Date.parse(q)); 

return friendly_time(d.getTime() / 1000);

 

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.