//返回 例 2017 const formatYear = data => { var date = data.date; var types = data.types; 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() if (types == 'year') { return [year].map(formatNumber).join('-') } else if (types == 'month') { return [month].map(formatNumber).join('-') } else if (types == 'day') { return [day].map(formatNumber).join('-') } } //返回 例 12月02日 const formatDate2 = date => { const month = date.getMonth() + 1 const day = date.getDate() return [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日' } //返回 例 2017-12-12 const formatDate = 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('-') } //返回 例 2017-12-12 12:30:00 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(':') } //返回 例 12-12 12:30:00 const formatTime2 = date => { const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':') } //返回 小时12:00:00 const formatHour = data => { var date = data.date; var types = data.types; const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() if (types == 'hour') { return [hour].map(formatNumber) } else if (types == 'minute') { return [minute].map(formatNumber).join('-') } else if (types == 'second') { return [second].map(formatNumber) } } //返回 指定秒数59 , 2017-12-12 12:30:59 function selfFormatTimeReturnSecond59(time) { let date = new Date(time); let year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() let hour = date.getHours() let minute = date.getMinutes() let second = date.getSeconds() return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, '59'].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } module.exports = { formatTime: formatTime, formatDate: formatDate, formatYear: formatYear, formatDate2: formatDate2, formatHour: formatHour, formatTime2: formatTime2, selfFormatTimeReturnSecond59 }