js 时间日期与时间戳之间转换

时间:2022-02-12 02:19:06
1.将时间(2017-08-10)转换时间戳
var startTime = '2017-08-10';
var startdate = new Date(Date.parse(startTime.replace(/-/g, "/")));

startdate = startdate.getTime() / 1000;
alert(startdate)

2.将时间(2017-08-10 10:15:30)转换时间戳
var startTime = '2017-08-10 10:15:30';
var startdate = new Date(Date.parse(startTime.replace(/-/g, "/")));
startdate = startdate.getTime() / 1000;
alert(startdate)

3.时间(2017/08/10)转换时间戳
var times = '2017/08/10';
var old = (new Date(times)).getTime()/1000;
alert(old);

4.时间(2017/08/10 15:15:30)转换时间戳
var times = '2017/08/10 15:15:30';
var old = (new Date(times)).getTime()/1000;
alert(old);