Home  >  Article  >  Web Front-end  >  Time conversion in js—sample code for converting milliseconds to date and time

Time conversion in js—sample code for converting milliseconds to date and time

PHP中文网
PHP中文网Original
2017-03-20 15:09:112022browse

Convert js millisecond time to date time

var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //得到毫秒数

Most of them use milliseconds divided by 365*24*60*60&1000 to convert it back. This method is too complicated to convert, year, month and day. , hours, minutes and seconds have to be obtained in different ways, and some years have 366 days, and some have 365 days, so it is too complicated to calculate.

I tried a method later and it actually worked

var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //得到毫秒数
var newTime = new Date(oldTime); //就得到普通的时间了

Directly pass in the milliseconds as a parameter and give it to the Date object to get the ordinary time, and then use getHours, getFullYear, etc. Method to get the year, month, day, hours, minutes and seconds

Related articles:

PHP converts seconds into days, hours and minutes

PHP convert seconds to days, hours and minutes format time

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn