DateUtils.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /** @format */
  2. interface DateConstructor {
  3. /**
  4. * 日期时间格式化
  5. * @param fmt 表达式
  6. * @param unTime 时间戳(秒级或者毫秒级)
  7. */
  8. Format(fmt: string, unTime?: number, isDHMS?: boolean): string
  9. /**
  10. * 毫秒转秒
  11. * @param ms 毫秒
  12. */
  13. msTurnSecond(ms: number): number
  14. /**
  15. * 秒转毫秒
  16. * @param ms 秒
  17. */
  18. secondTurnMs(s: number): number
  19. /**
  20. * 当前秒的时间戳
  21. */
  22. getSecondTime(timestamp?: number): number
  23. /**
  24. * 获取当前时间戳或传入时间戳的第dur分钟0秒的时间戳
  25. * @param timestamp 时间戳(秒级或者毫秒级)
  26. * @param dur 默认为下一分钟0秒,正数时间是未来时间,负数是过去时间
  27. */
  28. getMinuteTime(timestamp?: number, dur?: number): number
  29. /**
  30. * getMinuteTime的DHMS对象
  31. */
  32. getMinuteDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
  33. /**
  34. * 当前时间戳或传入时间戳的第dur小时0分0秒的时间戳
  35. * @param timestamp 时间戳(秒级或者毫秒级)
  36. * @param dur 默认为下一小时0分0秒,正数时间是未来时间,负数是过去时间
  37. */
  38. getHourTime(timestamp?: number, dur?: number): number
  39. /**
  40. * getHourTime的DHMS对象
  41. */
  42. getHourDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
  43. /**
  44. * 当前时间戳或传入时间戳的第dur天0点0分0秒的时间戳
  45. * @param timestamp 时间戳(秒级或者毫秒级)
  46. * @param dur 默认为下一天0时0分0秒,正数时间是未来时间,负数是过去时间
  47. */
  48. getDayTime(timestamp?: number, dur?: number): number
  49. /**
  50. * getDayTime的DHMS对象
  51. */
  52. getDayDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
  53. /**
  54. * 当前时间戳或传入时间戳的第dur周0点0分0秒的时间戳
  55. * @param timestamp 时间戳(秒级或者毫秒级)
  56. * @param dur 默认为1,下一周0点0分0秒,2为下下周一0点0分0秒,正数时间是未来时间,负数是过去时间
  57. */
  58. getWeekTime(timestamp?: number, dur?: number): number
  59. /**
  60. * getWeekTime的DHMS对象
  61. */
  62. getWeekDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
  63. /**
  64. * 当前时间戳或传入时间戳的第dur月0点0分0秒的时间戳
  65. * @param timestamp 时间戳(秒级或者毫秒级)
  66. * @param dur 默认为下一月0天0时0分0秒,正数时间是未来时间,负数是过去时间
  67. */
  68. getMouthTime(timestamp?: number, dur?: number): number
  69. /**
  70. * getMouthTime的DHMS对象
  71. */
  72. getMouthDHMS(timestamp?: number, dur?: number): {day: number; hour: number; minute: number; second: number}
  73. /**
  74. * 将秒数转换为天-小时-分钟-秒 返回JONS类型
  75. */
  76. date2DHMS(timestamp: number): {day: number; hour: number; minute: number; second: number}
  77. }
  78. Date.Format = (fmt: string, value?: number, isDHMS: boolean = false): string => {
  79. let tempDate
  80. if (value == undefined) {
  81. tempDate = new Date()
  82. } else {
  83. tempDate = new Date(Date.secondTurnMs(value))
  84. }
  85. let dhms
  86. if (isDHMS) dhms = Date.date2DHMS(value)
  87. const o = {
  88. 'M+': dhms ? 0 : tempDate.getMonth() + 1, // 月份
  89. 'd+': dhms ? dhms.day : tempDate.getDate(), // 日
  90. 'h+': dhms ? dhms.hour : tempDate.getHours(), // 小时
  91. 'm+': dhms ? dhms.minute : tempDate.getMinutes(), // 分
  92. 's+': dhms ? dhms.second : tempDate.getSeconds(), // 秒
  93. 'q+': dhms ? 0 : Math.floor((tempDate.getMonth() + 3) / 3), // 季度
  94. 'S+': dhms ? 0 : tempDate.getMilliseconds(), // 毫秒
  95. }
  96. if (/(y+)/.test(fmt)) {
  97. fmt = fmt.replace(RegExp.$1, `${tempDate.getFullYear()}`.substr(4 - RegExp.$1.length))
  98. }
  99. for (const k in o) {
  100. if (new RegExp(`(${k})`).test(fmt)) {
  101. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length))
  102. }
  103. }
  104. return fmt
  105. }
  106. Date.msTurnSecond = (ms: number): number => {
  107. //2050 年 1 月 1 日
  108. if (ms > 2524608000) {
  109. return Math.floor(ms / 1000)
  110. } else {
  111. return ms
  112. }
  113. }
  114. Date.secondTurnMs = (s: number): number => {
  115. //2050 年 1 月 1 日
  116. if (s <= 2524608000) {
  117. return s * 1000
  118. } else {
  119. return s
  120. }
  121. }
  122. Date.getSecondTime = (timestamp?: number) => {
  123. return Math.floor((timestamp ? timestamp : Date.now()) / 1000)
  124. }
  125. //转换时间年月日时分秒 --秒
  126. Date.date2DHMS = (secondTime: number) => {
  127. let oneDay = 86400
  128. let oneHour = 3600
  129. let oneMinut = 60
  130. let oneSecond = 1
  131. let day = Math.floor(secondTime / oneDay)
  132. let hour = Math.floor((secondTime - day * oneDay) / oneHour)
  133. let minute = Math.floor((secondTime - day * oneDay - hour * oneHour) / oneMinut)
  134. let second = Math.floor(secondTime - day * oneDay - hour * oneHour - minute * oneMinut)
  135. return {day, hour, minute, second}
  136. }
  137. let getSecondDur = (timestamp: number, nextTimeStamp: number) => {
  138. let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
  139. return Date.date2DHMS(Math.floor((nextTimeStamp - date.getTime()) / 1000))
  140. }
  141. Date.getMinuteTime = (timestamp?: number, dur?: number) => {
  142. let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
  143. dur = dur == undefined ? 1 : dur
  144. date.setMinutes(date.getMinutes() + dur)
  145. date.setSeconds(0, 0)
  146. return Date.msTurnSecond(date.getTime())
  147. }
  148. Date.getMinuteDHMS = (timestamp?: number, dur?: number) => {
  149. return getSecondDur(timestamp, Date.getMinuteTime(timestamp, dur))
  150. }
  151. Date.getHourTime = (timestamp?: number, dur?: number) => {
  152. let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
  153. dur = dur == undefined ? 1 : dur
  154. date.setHours(date.getHours() + dur)
  155. date.setMinutes(0, 0, 0)
  156. return Date.msTurnSecond(date.getTime())
  157. }
  158. Date.getHourDHMS = (timestamp?: number, dur?: number) => {
  159. return getSecondDur(timestamp, Date.getHourTime(timestamp, dur))
  160. }
  161. Date.getDayTime = (timestamp?: number, dur?: number) => {
  162. let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
  163. dur = dur == undefined ? 1 : dur
  164. date.setDate(date.getDate() + dur) // 将当前日期增加1天
  165. date.setHours(0, 0, 0, 0) // 将小时、分钟、秒和毫秒部分都设为0,得到下一天的零点时间
  166. return Date.msTurnSecond(date.getTime()) // 返回下一天零点的时间戳
  167. }
  168. Date.getDayDHMS = (timestamp?: number, dur?: number) => {
  169. return getSecondDur(timestamp, Date.getDayTime(timestamp, dur))
  170. }
  171. Date.getWeekTime = (timestamp?: number, dur?: number) => {
  172. let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
  173. dur = dur == undefined ? 1 : dur
  174. let day = date.getDay()
  175. let diff = day === 0 ? 1 : 8 - day // 计算距离当前或下一个周一的天数
  176. // 计算目标周一的日期
  177. date.setDate(date.getDate() + diff + Math.floor(dur) * 7)
  178. date.setHours(0, 0, 0, 0) // 将小时、分钟、秒和毫秒部分都设为0,得到下一周的零点时间
  179. return Date.msTurnSecond(date.getTime()) // 返回下一周零点的时间戳(秒级)
  180. }
  181. Date.getWeekDHMS = (timestamp?: number, dur?: number) => {
  182. return getSecondDur(timestamp, Date.getWeekTime(timestamp, dur))
  183. }
  184. Date.getMouthTime = (timestamp?: number, dur?: number) => {
  185. let date = new Date(timestamp ? Date.secondTurnMs(timestamp) : Date.now())
  186. dur = dur == undefined ? 1 : dur
  187. date.setMonth(date.getMonth() + dur)
  188. date.setDate(1)
  189. date.setHours(0, 0, 0, 0)
  190. return Date.msTurnSecond(date.getTime())
  191. }
  192. Date.getMouthDHMS = (timestamp?: number, dur?: number) => {
  193. return getSecondDur(timestamp, Date.getMouthTime(timestamp, dur))
  194. }