const formatDate = (date) => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return [year, month, day].map(formatNumber).join('-');
}
const formatDate_cn = (date) => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return month + '月' + day + '日';
}
const formatTime = function (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 formatTime_interval = function (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].map(formatNumber).join(':')
}
const formatTime_intervalTime = function (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 [hour, minute].map(formatNumber).join(':')
}
const formatTime_cn = date => {
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
var today = new Date().getDate()
var dayText = formatDate(date)
if (day == today) {
dayText = '今天';
} else if (day == today - 1) {
dayText = '昨天';
} else if (day == today - 2) {
dayText = '前天';
}
return (dayText + ' ' + [hour, minute].map(formatNumber).join(':'))
}
const timetransform = time => {
var seconds = numtransform(time % 60)
var minutes = numtransform(Math.floor(time / 60))
var hours = numtransform(Math.floor(time / 60 / 60))
return ((hours > 0 ? hours + ':' : '') + minutes + ":" + seconds)
}
//时间转换
const numtransform = num => {
if (num < 10) {
return "0" + num
} else {
return num
}
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatDate: formatDate, //new Date(time) 转换成2018-11-11
formatDate_cn: formatDate_cn,//new Date(time) 转换成2018年11月11日
formatTime: formatTime,//new Date(time) 转换成2018-11-11 11:11:11
formatTime_interval: formatTime_interval,//new Date(time) 转换成2018/11/11 11:11
timetransform: timetransform, //113秒转00:00:00
formatTime_cn: formatTime_cn, //new Date(time)2018-12-12T09:26:28.000Z 获取今天/昨天/前天/日期+13:33
formatTime_intervalTime: formatTime_intervalTime, //转换成00:00 时间
}
阅读(1324) | 评论(0) | 转发(0) |