Home  >  Article  >  Web Front-end  >  js time to millisecond conversion

js time to millisecond conversion

不言
不言Original
2018-07-09 17:19:583437browse

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

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)

2) Convert milliseconds to time

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

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!

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