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

How to convert time to milliseconds in javascript

藏色散人
Release: 2021-11-09 14:23:18
Original
7172 people have browsed it

Javascript method to convert time to milliseconds: 1. Convert date to milliseconds through "(new Date(startDate)).getTime();" 2. Convert date to milliseconds through "Date.parse(new Date(arr[ 0]...)" method to convert the date into milliseconds.

How to convert time to milliseconds in javascript

The operating environment of this article: Windows 7 system, javascript version 1.8.5, Dell G3 computer.

javascript How to convert time to milliseconds?

Convert date to milliseconds

If the format is: yyyy/mm/dd hh: mm:ss 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 be converted into 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 dates into 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

Recommended learning: "javascript Basic Tutorial

The above is the detailed content of How to convert time to milliseconds in javascript. 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!