const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') }
const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } const formatDate = date =>{ const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return [year, month, day].map(formatNumber).join('-'); } //日期加 const addDate = (date, days)=>{ let nowDate = new Date(date); nowDate.setDate(nowDate.getDate() + days); let nowMonth = nowDate.getMonth() + 1; let nowDay = nowDate.getDate(); if (nowMonth < 10) { nowMonth = "0" + nowMonth; } if (nowDay < 10) { nowDay = "0" + nowDay; } let traDate = nowDate.getFullYear() + "-" + nowMonth + "-" + nowDay; return traDate; } //日期减 const reduceDate = (date, days) => { let nowDate = new Date(date); nowDate.setDate(nowDate.getDate() - days); let nowMonth = nowDate.getMonth() + 1; let nowDay = nowDate.getDate(); if (nowMonth < 10) { nowMonth = "0" + nowMonth; } if (nowDay < 10) { nowDay = "0" + nowDay; } let traDate = nowDate.getFullYear() + "-" + nowMonth + "-" + nowDay; return traDate; } module.exports = { formatTime: formatTime, addDate, reduceDate, formatDate }