/*
module:时间处理模块
*/
//初始化时间
const create = (specific, around) => {
let day = specific ? new Date(specific) : new Date();
(specific && around) ? console.warn(`传递了具体时间${specific},再传递前后时间${around < 0 ? ('提前' + around * -1) : ('往后' + around)}天,结果不会结合起来,只会返回具体时间`) : day.setDate(day.getDate() + around)
return day
}
//初始化时间,得到具体年,月,日,时,分,秒,星期
const timeFormat = (time, weekArr, isZero = true) => {
let year = time.getFullYear()
let month = time.getMonth() + 1;
month = (isZero && month <= 9) ? "0" + month : month
let day = (isZero && time.getDate() <= 9) ? "0" + time.getDate() : time.getDate()
let hour = (isZero && time.getHours() <= 9) ? "0" + time.getHours() : time.getHours()
let minutes = (isZero && time.getMinutes() <= 9) ? "0" + time.getMinutes() : time.getMinutes()
let seconds = (isZero && time.getSeconds() <= 9) ? "0" + time.getSeconds() : time.getSeconds()
let week = weekArr[time.getDay()];
return { year, month, day, hour, minutes, seconds, week }
}
//根据具体格式返回时间
const timeStringFormat = (str, date, weekArr) => {
const dateTimeParts = str.split(/[ :-]/);//符号
const separators = str.match(/[^\d\w\s]+/g);//分割符
const spaces = str.match(/\s+/g) || [] //空格
let time = timeFormat(date, weekArr, dateTimeParts.every(item => item % 2 == 0))
let resArr = ['year', 'month', 'day', 'hour', 'minutes', 'seconds', 'week']
let arr = resArr.splice(0, dateTimeParts.length)
spaces[0] && separators.splice(2, 0, spaces[0])
spaces[0] && separators.push(spaces[1] || '')
let res = arr.reduce(function (total, item, index, arr) {
return total + time[item] + (separators[index] || '')
}, '')
return res
}
/**
* @class 获取时间
* @description 处理时间格式,
* @param {String} specific - 具体某一时间
* @param {Number} around - 当天的前一天还是后一天,-1为前一天,1为后一天,其余类似
* @return {function} 不同需求时间处理函数
* @author juZiBuTian 2023/12/08
* @version 1.0.0
* @example
* new class('20123-12-08',0)
*/
class Time {
constructor(specific = '', around = 0) {
this.date = create(specific, around)
this.weekArr = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
]
}
/**
* @function 获取时间戳
* @description 得到时间戳,用于(生成id等需求)
* @param {undefined} null - 无
* @return {Number} 返回13位时间戳
* @author juZiBuTian 2023/12/08
* @version 1.0.0
* @example
* new class('20123-12-08',0).timeStamp()
*/
timeStamp() {
return this.date.getTime()
}
/**
* @function 获取特定时间格式字符串
* @description 通过传递函数返回想要的时间格式,如果格式同一,可以写死返回时间格式,里面有 year, month, day, hour, minutes, seconds, week
* @param {function || string} collback 处理时间格式函数 || 详细格式xxxx-xx-xx
* @return {String} 返回回调函数结果
* @author juZiBuTian 2023/12/08
* @version 1.0.0
* @example
* new class('2023-12-08',0).timeStamp()
*/
format(collback) {
let time = timeFormat(this.date, this.weekArr)
if (!collback) {
let { year, month, day } = time
return `${year}-${month}-${day}`
} else {
let res = {
"function": () => collback(time),
"string": () => timeStringFormat(collback, this.date, this.weekArr)
}
if (res[typeof collback]) {
return res[typeof collback]()
} else {
console.warn('目前只能传递 String 和 function 类型,你传递类型没有处理,所以返回一种指定格式')
let { year, month, day, hour, minutes, seconds, week } = time
return `${year}-${month}-${day} ${hour}:${minutes}:${seconds} ${week}`
}
}
}
/**
* @function 获取两个时间的间隔
* @description 通过传递的两个时间得到两个时间之间相隔 天,小时,分,秒
* @param {string} startDate 开始时间
* @param {string} endDate 结束时间,默认为当天
* @return {String} 返回相隔的时间
* @author juZiBuTian 2023/12/08
* @version 1.0.0
* @example
* class.phase('2023-12-08')
*/
static phase(startDate, endDate = '') {
let startDateVal = new Time(startDate).timeStamp()
let endDateVal = new Time(endDate).timeStamp()
let seconds = Math.round(Math.abs((startDateVal - endDateVal) / 1000));
const DAY = 86400;
const HOUR = 3600;
const MINUTE = 60;
let days = Math.floor(seconds / DAY);
seconds -= days * DAY;
let hours = Math.floor(seconds / HOUR);
seconds -= hours * HOUR;
let minutes = Math.floor(seconds / MINUTE);
seconds -= minutes * MINUTE;
return `${days}天 ${hours}小时 ${minutes}分钟 ${seconds}秒`;
}
/**
* @function 获取当月最后一天和第一天
* @description 通过传递 firse || last 分别获取当月第一天或者最后一天
* @param {string} day 第一天或者最后一天
* @return {Object} 返回时间格式,里面有 year, month, day, hour, minutes, seconds, week
* @author juZiBuTian 2023/12/08
* @version 1.0.0
* @example
* class.dayOfMonth('last')
*/
static dayOfMonth(day = 'first',isZero = true){
let data = new Date()
let firstDayOfMonth = new Date(data.getFullYear(), data.getMonth(), 1);
let firstDayOfMonthTime = firstDayOfMonth.getTime();
let lastDayOfMonth = new Date(data.getFullYear(), data.getMonth() + 1, 0);
let lastDayOfMonthTime = lastDayOfMonth.getTime();
let time = new Date(day == 'first'? firstDayOfMonthTime: day == 'last'? lastDayOfMonthTime: '')
time = timeFormat(time, new Time().weekArr, isZero)
return time
}
}
超级详细时间处理,各种需求都能实现,拜谢评论指导
于 2023-12-11 18:06:02 首次发布