获取 yyyy-mm-dd 格局
function getDate(date){var time = new Date(date)
var year = time.getFullYear() // 年
var month = ('0' + (time.getMonth() + 1)).slice(-2) // 月
var day = ('0' + time.getDate()).slice(-2) // 日
var mydate = year + '-' + month + '-' + day
return mydate
}
如果也需获取 hh:mm:ss
function gettime(date){var time=new Date(date)
var hours = (time.getHours()) > 9 ? (time.getHours()) : "0" + (time.getHours());
var minutes = (time.getMinutes()) > 9 ? (time.getMinutes()) : "0" + (time.getMinutes());
var seconds = (time.getSeconds()) > 9 ? (time.getSeconds()) : "0" + (time.getSeconds());
return hours+minutes+seconds
}
如果应用 elementUI 能够间接将工夫空间的格局 format 设置为‘yyyy-MM-dd HH:mm:ss’
yyyy-mm-dd 转 yyyymmdd
function replaceTime(str){
var reg= /-/g
return str.replace(reg, '')
}
如 replaceTime(‘2021-06-01’) 输入为 20210601
yyyymmdd 转 yyyy-mm-dd
function replaceTime(str){var pattern = /(\d{4})(\d{2})(\d{2})/ // 输入正则
return str.replace(pattern, '$1-$2-$3')
}
如 replaceTime(‘20210601’) 输入为 2021-06-01