194 lines
6.0 KiB
JavaScript
194 lines
6.0 KiB
JavaScript
|
||
export const MILLISECOND_PER_SECOND = 1000
|
||
export const MILLISECOND_PER_MINUTE = 60 * MILLISECOND_PER_SECOND
|
||
export const MILLISECOND_PER_HOUR = 60 * MILLISECOND_PER_MINUTE
|
||
export const MILLISECOND_PER_DAY = 24 * MILLISECOND_PER_HOUR
|
||
export const MILLISECOND_PER_MONTH = 30 * MILLISECOND_PER_DAY
|
||
export const MILLISECOND_PER_YEAR = 365 * MILLISECOND_PER_MONTH
|
||
|
||
function addZero (t) {
|
||
if (t < 10) {
|
||
t = '0' + t
|
||
}
|
||
return t
|
||
}
|
||
|
||
/**
|
||
* @description: 输入Date或者时间戳,返回0时0分0秒的Date(非UTC)
|
||
* @param {Date | milliseconds} 必选
|
||
* @return: {Date} 返回0时0分0秒的Date
|
||
*/
|
||
export function clearTime (date) {
|
||
let d = new Date(date)
|
||
if (d.toString() === 'Invalid Date') {
|
||
return false
|
||
} else {
|
||
d.setHours(0)
|
||
d.setMinutes(0)
|
||
d.setSeconds(0)
|
||
d.setMilliseconds(0)
|
||
return d
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description: 输入Date对象或者时间戳,返回 YYYY-MM-DD (非UTC)格式的字符串,分隔符默认 - ,可自定义
|
||
* @param {Date | milliseconds} 必选
|
||
* @param {String} 可选,用于年月日之间的分隔符
|
||
* @return: {String} 返回 YYYY-MM-DD 格式的字符串
|
||
*/
|
||
export function tranTimeMD (time, split = '-') {
|
||
let d = new Date(time)
|
||
if (d.toString() === 'Invalid Date') {
|
||
return false
|
||
} else {
|
||
let month = d.getMonth() + 1
|
||
let date = d.getDate()
|
||
month = addZero(month)
|
||
date = addZero(date)
|
||
let timeYMD = month + split + date
|
||
return timeYMD
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description: 输入Date对象或者时间戳,返回 YYYY-MM-DD (非UTC)格式的字符串,分隔符默认 - ,可自定义
|
||
* @param {Date | milliseconds} 必选
|
||
* @param {String} 可选,用于年月日之间的分隔符
|
||
* @return: {String} 返回 YYYY-MM-DD 格式的字符串
|
||
*/
|
||
export function tranTimeYMD (time, split = '-') {
|
||
let d = new Date(time)
|
||
if (d.toString() === 'Invalid Date') {
|
||
return false
|
||
} else {
|
||
let month = d.getMonth() + 1
|
||
let date = d.getDate()
|
||
month = addZero(month)
|
||
date = addZero(date)
|
||
let timeYMD = d.getFullYear() + split + month + split + date
|
||
return timeYMD
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description: 输入Date对象或者时间戳,返回 HH:MM:SS (非UTC)格式的字符串,分隔符默认 : ,可自定义
|
||
* @param {Date | milliseconds} 必选
|
||
* @param {String} 可选,用于时分秒之间的分隔符
|
||
* @return: {String} 返回 HH:MM:SS 格式的字符串
|
||
*/
|
||
export function tranTimeHMS (time, split = ':') {
|
||
let d = new Date(time)
|
||
if (d.toString() === 'Invalid Date') {
|
||
return false
|
||
} else {
|
||
let hours = addZero(d.getHours())
|
||
let minutes = addZero(d.getMinutes())
|
||
let seconds = addZero(d.getSeconds())
|
||
let timeHMS = hours + split + minutes + split + seconds
|
||
return timeHMS
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description: 输入Date对象或者时间戳,返回 YYYY-MM-DD HH:MM:SS (非UTC)格式的字符串,分隔符可自定义
|
||
* @param {Date | milliseconds} 必选
|
||
* @param {String} 可选,用于年月日之间的分隔符
|
||
* @param {String} 可选,用于时分秒之间的分隔符
|
||
* @return: {String} 返回 YYYY-MM-DD HH:MM:SS 格式的字符串
|
||
*/
|
||
export function tranTimeYMDHMS (time, split, split2, split3 = ' ') {
|
||
let d = new Date(time)
|
||
if (d.toString() === 'Invalid Date') {
|
||
return false
|
||
} else {
|
||
return tranTimeYMD(time, split) + split3 + tranTimeHMS(time, split2)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description: 传入时间(毫秒),转化为对应的中文
|
||
* @param {Number} 必选,输入毫秒
|
||
* @return: {String} , 返回对应的中文时间
|
||
*/
|
||
export function getTimeUnit (timestamps) {
|
||
if (typeof timestamps !== 'number' || timestamps <= 0) {
|
||
return '0秒'
|
||
}
|
||
if (timestamps >= MILLISECOND_PER_YEAR) {
|
||
return parseInt(timestamps / MILLISECOND_PER_YEAR) + '年'
|
||
}
|
||
if (timestamps >= MILLISECOND_PER_MONTH) {
|
||
return parseInt(timestamps / MILLISECOND_PER_MONTH) + '个月'
|
||
}
|
||
if (timestamps >= MILLISECOND_PER_DAY) {
|
||
return parseInt(timestamps / MILLISECOND_PER_DAY) + '天'
|
||
}
|
||
if (timestamps >= MILLISECOND_PER_HOUR) {
|
||
return parseInt(timestamps / MILLISECOND_PER_HOUR) + '小时'
|
||
}
|
||
if (timestamps >= MILLISECOND_PER_MINUTE) {
|
||
return parseInt(timestamps / MILLISECOND_PER_MINUTE) + '分钟'
|
||
}
|
||
if (timestamps >= MILLISECOND_PER_SECOND) {
|
||
return parseInt(timestamps / MILLISECOND_PER_SECOND) + '秒'
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description: 将时间日期转化为时间戳(支持格式 2017-12-12 12:12:12)
|
||
* @param {Date} 需要转化的时间
|
||
* @return: {milliseconds} 返回对应时间的时间戳
|
||
*/
|
||
export function timeToStamps (time) {
|
||
return new Date(time).getTime()
|
||
}
|
||
|
||
function getSeconds (milliseconds) {
|
||
let sec = parseInt(milliseconds / MILLISECOND_PER_SECOND)
|
||
return sec ? `${sec}秒` : ''
|
||
}
|
||
function getMinutes (milliseconds) {
|
||
if (milliseconds / MILLISECOND_PER_MINUTE >= 1) {
|
||
return `${Math.floor(milliseconds / MILLISECOND_PER_MINUTE)}分${getSeconds(milliseconds % MILLISECOND_PER_MINUTE)}`
|
||
} else {
|
||
return getSeconds(milliseconds)
|
||
}
|
||
}
|
||
function getHours (milliseconds) {
|
||
if (milliseconds / MILLISECOND_PER_HOUR >= 1) {
|
||
return `${Math.floor(milliseconds / MILLISECOND_PER_HOUR)}小时${getMinutes(milliseconds % MILLISECOND_PER_HOUR)}`
|
||
} else {
|
||
return getMinutes(milliseconds)
|
||
}
|
||
}
|
||
function getDays (milliseconds) {
|
||
if (milliseconds / MILLISECOND_PER_DAY >= 1) {
|
||
return `${Math.floor(milliseconds / MILLISECOND_PER_DAY)}天${getHours(milliseconds % MILLISECOND_PER_DAY)}`
|
||
} else {
|
||
return getHours(milliseconds)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 将毫秒值转换成x时x分x秒的形式
|
||
* @param {Number} timestamps - 时间的毫秒形式
|
||
* @return {String} str - 转换后的字符串
|
||
*/
|
||
export function convertMStoString (timestamps = 0) {
|
||
if (typeof timestamps !== 'number' || timestamps <= 0) {
|
||
return '0秒'
|
||
}
|
||
return getDays(Math.floor(timestamps))
|
||
}
|
||
|
||
/**
|
||
* 将秒值转换成x时x分x秒的形式
|
||
* @param {Number} time - 时间的秒形式
|
||
* @return {String} str - 转换后的字符串
|
||
*/
|
||
export function convertSecToString (time = 0) {
|
||
let timestamps = typeof time !== 'number' ? 0 : time * MILLISECOND_PER_SECOND
|
||
return convertMStoString(timestamps)
|
||
}
|