1、时间字符串转成时间
currtime = "2019-01-02 10:08:00";
var d = new Date(currtime); //这样currtime就转化成时间d了
2、将时间转成字符串
这里介绍一个自字义的时间转化成字符串函数
function dateToString(date){
var year = date.getFullYear();
var month =(date.getMonth() + 1).toString();
var day = (date.getDate()).toString();
if (month.length == 1) {
month = "0" + month;
}
if (day.length == 1) {
day = "0" + day;
}
var hours = date.getHours().toString();
if(hours.length == 1){
hours = "0" + hours;
}
var mintus = date.getMinutes().toString();
if(mintus.length == 1){
mintus = "0" + mintus;
}
var second = date.getSeconds().toString();
if(second.length == 1){
second = "0" + second;
}
var dateTime = year + "-" + month + "-" + day + " " + hours + ":" + mintus + ":" + second;
return dateTime;
}
使用方法
alert(dateToString(d)); //d为上面的时间
1 【2】