Use JavaScript to convert time to today, yesterday, the day before, etc. format

Source: Internet
Author: User
Tags getdate min

This article mainly introduces the use of JavaScript to convert time to today, yesterday, the day before, and other formats of relevant information, the need for friends can refer to the

The method is super simple, the time format is good, directly to the code

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 function transdate () {var $time =document.getelementbyid ("Sha Re-time "); var date = $time. Innerhtml.trim (); var tt = new Date (parseint (date)); var days = parseint (new Date (). GetTime ()-date)/86400000; var today = new Date (). GetDate (); var year = Tt.getfullyear (); var mouth = tt.getmonth () + 1; var day = Tt.getdate (); var time = tt.gethours () < 10? "0" + tt.gethours (): Tt.gethours (); var min = tt.getminutes () < 10? "0" + tt.getminutes (): Tt.getminutes ();        var result, offset;     offset = Math.Abs (today-day); if (days < 4&&offset<4) {if (offset = 0) {result = "Today" + Time + ":" + min;} else if (offset = = 1) {result = "Yesterday" + Time + ":" + min;} else if (offset = = 2) {result = "day before yesterday" + Time + ":" + min;}} else {result = year + '-"+ Mouth +"-"+ Day +" "+Time + ":" + min;} $time. InnerHTML = result; } transdate ();

The above is the entire contents of this article, I hope you can enjoy.

Related Article

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.