Home  >  Article  >  Web Front-end  >  How to convert time to milliseconds in javascript

How to convert time to milliseconds in javascript

藏色散人
藏色散人Original
2021-11-09 14:11:317169browse

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(); //得到毫秒数

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)

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!

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