Home >Web Front-end >JS Tutorial >How to convert time to milliseconds in javascript
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.
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!