123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- /** @format */
- interface DateConstructor {
- /**
- * 日期时间格式化
- * @param fmt 表达式
- * @param unTime 时间戳(秒级或者毫秒级)
- */
- Format(fmt: string, unTime?: number, isDHMS?: boolean): string
- /**
- * 毫秒转秒
- * @param ms 毫秒
- */
- msTurnSecond(ms: number): number
- /**
- * 秒转毫秒
- * @param ms 秒
- */
- secondTurnMs(s: number): number
- /**
- * 当前秒的时间戳
- */
- getSecondTime(timestamp?: number): number
- /**
- * 获取当前时间戳或传入时间戳的第dur分钟0秒的时间戳
- * @param timestamp 时间戳(秒级或者毫秒级)
- * @param dur 默认为下一分钟0秒,正数时间是未来时间,负数是过去时间
- */
- getMinuteTime(timestamp?: number, dur?: number): number
- /**
- * getMinuteTime的DHMS对象
- */
- getMinuteDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
- /**
- * 当前时间戳或传入时间戳的第dur小时0分0秒的时间戳
- * @param timestamp 时间戳(秒级或者毫秒级)
- * @param dur 默认为下一小时0分0秒,正数时间是未来时间,负数是过去时间
- */
- getHourTime(timestamp?: number, dur?: number): number
- /**
- * getHourTime的DHMS对象
- */
- getHourDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
- /**
- * 当前时间戳或传入时间戳的第dur天0点0分0秒的时间戳
- * @param timestamp 时间戳(秒级或者毫秒级)
- * @param dur 默认为下一天0时0分0秒,正数时间是未来时间,负数是过去时间
- */
- getDayTime(timestamp?: number, dur?: number): number
- /**
- * getDayTime的DHMS对象
- */
- getDayDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
- /**
- * 当前时间戳或传入时间戳的第dur周0点0分0秒的时间戳
- * @param timestamp 时间戳(秒级或者毫秒级)
- * @param dur 默认为1,下一周0点0分0秒,2为下下周一0点0分0秒,正数时间是未来时间,负数是过去时间
- */
- getWeekTime(timestamp?: number, dur?: number): number
- /**
- * getWeekTime的DHMS对象
- */
- getWeekDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
- /**
- * 当前时间戳或传入时间戳的第dur月0点0分0秒的时间戳
- * @param timestamp 时间戳(秒级或者毫秒级)
- * @param dur 默认为下一月0天0时0分0秒,正数时间是未来时间,负数是过去时间
- */
- getMouthTime(timestamp?: number, dur?: number): number
- /**
- * getMouthTime的DHMS对象
- */
- getMouthDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
- /**
- * 将秒数转换为天-小时-分钟-秒 返回JONS类型
- */
- date2DHMS(timestamp: number): {day: number; hour: number; minute: number; second: number}
- }
- Date.Format = (fmt: string, value?: number, isDHMS: boolean = false): string => {
- let tempDate
- if (value == undefined) {
- tempDate = new Date()
- } else {
- tempDate = new Date(Date.secondTurnMs(value))
- }
- let dhms
- if (isDHMS) dhms = Date.date2DHMS(value)
- const o = {
- 'M+': dhms ? 0 : tempDate.getMonth() + 1, // 月份
- 'd+': dhms ? dhms.day : tempDate.getDate(), // 日
- 'h+': dhms ? dhms.hour : tempDate.getHours(), // 小时
- 'm+': dhms ? dhms.minute : tempDate.getMinutes(), // 分
- 's+': dhms ? dhms.second : tempDate.getSeconds(), // 秒
- 'q+': dhms ? 0 : Math.floor((tempDate.getMonth() + 3) / 3), // 季度
- 'S+': dhms ? 0 : tempDate.getMilliseconds(), // 毫秒
- }
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, `${tempDate.getFullYear()}`.substr(4 - RegExp.$1.length))
- }
- for (const k in o) {
- if (new RegExp(`(${k})`).test(fmt)) {
- fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length))
- }
- }
- return fmt
- }
- Date.msTurnSecond = (ms: number): number => {
- //2050 年 1 月 1 日
- if (ms > 2524608000) {
- return Math.floor(ms / 1000)
- } else {
- return ms
- }
- }
- Date.secondTurnMs = (s: number): number => {
- //2050 年 1 月 1 日
- if (s <= 2524608000) {
- return s * 1000
- } else {
- return s
- }
- }
- Date.getSecondTime = (timestamp?: number) => {
- return Math.floor((timestamp ? timestamp : Date.now()) / 1000)
- }
- //转换时间年月日时分秒 --秒
- Date.date2DHMS = (secondTime: number) => {
- let oneDay = 86400
- let oneHour = 3600
- let oneMinut = 60
- let oneSecond = 1
- let day = Math.floor(secondTime / oneDay)
- let hour = Math.floor((secondTime - day * oneDay) / oneHour)
- let minute = Math.floor((secondTime - day * oneDay - hour * oneHour) / oneMinut)
- let second = Math.floor(secondTime - day * oneDay - hour * oneHour - minute * oneMinut)
- return {day, hour, minute, second}
- }
- let getSecondDur = (timestamp: number, nextTimeStamp: number) => {
- let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
- return Date.date2DHMS(Math.floor((nextTimeStamp - date.getTime()) / 1000))
- }
- Date.getMinuteTime = (timestamp?: number, dur?: number) => {
- let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
- dur = dur == undefined ? 1 : dur
- date.setMinutes(date.getMinutes() + dur)
- date.setSeconds(0, 0)
- return Date.msTurnSecond(date.getTime())
- }
- Date.getMinuteDHMS = (timestamp?: number, dur?: number) => {
- return getSecondDur(timestamp, Date.getMinuteTime(timestamp, dur))
- }
- Date.getHourTime = (timestamp?: number, dur?: number) => {
- let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
- dur = dur == undefined ? 1 : dur
- date.setHours(date.getHours() + dur)
- date.setMinutes(0, 0, 0)
- return Date.msTurnSecond(date.getTime())
- }
- Date.getHourDHMS = (timestamp?: number, dur?: number) => {
- return getSecondDur(timestamp, Date.getHourTime(timestamp, dur))
- }
- Date.getDayTime = (timestamp?: number, dur?: number) => {
- let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
- dur = dur == undefined ? 1 : dur
- date.setDate(date.getDate() + dur) // 将当前日期增加1天
- date.setHours(0, 0, 0, 0) // 将小时、分钟、秒和毫秒部分都设为0,得到下一天的零点时间
- return Date.msTurnSecond(date.getTime()) // 返回下一天零点的时间戳
- }
- Date.getDayDHMS = (timestamp?: number, dur?: number) => {
- return getSecondDur(timestamp, Date.getDayTime(timestamp, dur))
- }
- Date.getWeekTime = (timestamp?: number, dur?: number) => {
- let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
- dur = dur == undefined ? 1 : dur
- let day = date.getDay()
- let diff = day === 0 ? 1 : 8 - day // 计算距离当前或下一个周一的天数
- // 计算目标周一的日期
- date.setDate(date.getDate() + diff + Math.floor(dur) * 7)
- date.setHours(0, 0, 0, 0) // 将小时、分钟、秒和毫秒部分都设为0,得到下一周的零点时间
- return Date.msTurnSecond(date.getTime()) // 返回下一周零点的时间戳(秒级)
- }
- Date.getWeekDHMS = (timestamp?: number, dur?: number) => {
- return getSecondDur(timestamp, Date.getWeekTime(timestamp, dur))
- }
- Date.getMouthTime = (timestamp?: number, dur?: number) => {
- let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
- dur = dur == undefined ? 1 : dur
- date.setMonth(date.getMonth() + dur)
- date.setDate(1)
- date.setHours(0, 0, 0, 0)
- return Date.msTurnSecond(date.getTime())
- }
- Date.getMouthDHMS = (timestamp?: number, dur?: number) => {
- return getSecondDur(timestamp, Date.getMouthTime(timestamp, dur))
- }
|