一、日期转时间戳(精确到秒)
var date = new Date("2018-11-20 18:08:00");//得到一个Date格式的时间;
var time = date.getTime()/1000;//转换成秒;
二、时间戳转换成日期(精确到秒)
function formateTimeStamp(time) {
var date = new Date(time*1000);
var year = date.getFullYear();
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
//调用方法得到日期格式
formateTimeStamp(time)//将一个时间戳转换成日期带分秒格式(2022-11-07 15:06:50)