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.