102 lines
2.9 KiB
JavaScript
Raw Normal View History

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