1、yyyy-MM-dd转时间戳
var date = '1999-09-21'
var res = Date.parse(new Date(date))/1000 //937872000
2、标准时间转yyyy-MM-dd
var res = new Date().toLocaleDateString().slice().replace(/\//g, '-') //"2020-9-10"
3、标准时间转 yyyy-MM-dd HH:mm:ss
var date= new Date() //Thu Sep 10 2020 17:22:32 GMT+0800 (中国标准时间)
var year=date.getFullYear(); //取得4位数的年份
var month=date.getMonth()+1; //取得日期中的月份,其中0表示1月,11表示12月
var day=date.getDate(); //返回日期月份中的天数(1到31)
var hour=date.getHours(); //返回日期中的小时数(0到23)
var minute=date.getMinutes(); //返回日期中的分钟数(0到59)
var second=date.getSeconds(); //返回日期中的秒数(0到59)
var res = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; //2020-9-10 17:22:32
4、时间戳转yyyy-MM-dd HH:mm:ss
var date= new Date(937872000 ) //标准时间
//先把时间戳转为标准时间,然后再执行3中步骤
5、时间戳转时间
var timestamp = 937872000 //1中获取的时间戳
var res = new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ") // 1999/9/21 上午8:00:00
var timestamp1 = Date.parse(new Date) //精确到秒 1599730486000
var timestamp2=new Date().getTime() //精确到毫秒 1599730486864
var res2 = new Date(parseInt(timestamp1)).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ") // 结果都是2020/9/10 下午5:32:01
6、时间戳转年月日时分秒
将4和3结合后中最后一句变为
var res = year+"年"+month+"月"+day+"日 "+hour+"时"+minute+"分"+second+"秒" // 2020年9月10日 17时57分8秒