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.