Home > Web Front-end > JS Tutorial > body text

js time to millisecond conversion

不言
Release: 2018-07-09 17:19:58
Original
3447 people have browsed it

This article mainly introduces the conversion between js time and milliseconds. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

1) Convert date to milliseconds

If the format is: yyyy/mm/dd hh:mm:ss, it can be converted directly. var oldTime = (new Date("2018/07/09 14:13:11")).getTime(); //Get the number of milliseconds

If the date format is: yyyy-mm-dd hh:mm :ss needs to convert the format

var startDate ='2018-07-09 14:13:11';
    startDate= startDate.replace(new RegExp("-","gm"),"/");
    var startDateM = (new Date(startDate)).getTime(); //得到毫秒数
Copy after login

Another way to convert date to milliseconds:

var str = '2018-07-09 14:13:11';
var arr = str.split(/[- : \/]/);
var startDate = Date.parse(new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]));
console.log(startDate)
Copy after login

2) Convert milliseconds to time

var endDate = (new Date("2018/072/09 14:13:11")).getTime(); //得到毫秒数  
var newDate = new Date(endDate ); //得到普通的时间了
Copy after login

The above is the content of this article All content, I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

js moves any element to a specified position

About the implementation of JS effect function

The above is the detailed content of js time to millisecond conversion. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!