Home > Web Front-end > JS Tutorial > 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中文网
Release: 2017-03-20 15:09:11
Original
2055 people have browsed it

Convert js millisecond time to date time

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

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); //就得到普通的时间了
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template