Time conversion in js sample code for converting milliseconds to date times

  • 2020-03-30 01:28:50
  • OfStack

Js millisecond time converted to date time


var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //You get the number of milliseconds

Most of them divide the number of milliseconds by 365 times 24 times 60 times 60 and 1000, and then you go back, and it's too complicated to do that, you have to do it in a different way, year, month, day, hour, and second, and some years have 366 days, some 365 days, and it's too complicated to do that.

Later, I tried a method, and it worked


var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //You get the number of milliseconds
var newTime = new Date(oldTime); //I get the normal time

Directly pass in the number of milliseconds as a parameter, to the Date object can get the ordinary time, and then through getHours,getFullYear and other methods to get the Date, minutes and seconds


Related articles: