Use javascript to convert the time format to today, yesterday, and the day before yesterday.

Source: Internet
Author: User

Use javascript to convert the time format to today, yesterday, and the day before yesterday.

This article mainly introduces how to use javascript to convert the time into the format of today, yesterday, and the day before yesterday. For more information, see

The method is super simple. Just format the time and provide the Code directly.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Function transDate (){

Var $ time = document. getElementById ("share-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 = "the day before yesterday" + time + ":" + min;

}

} Else {

Result = year + "-" + mouth + "-" + day + "" + time + ":" + min;

}

$ Time. innerHTML = result;

}

TransDate ();

The above is all the content of this article. I hope you will like it.

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.