date.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @class DateTime
  3. * @description 日期处理模块
  4. */
  5. module.exports = class DateTime {
  6. constructor() {
  7. //默认日期展示格式
  8. this.defaultDateFormat = 'Y-m-d H:i:s'
  9. //默认时区
  10. this.defaultTimezone = 8
  11. this.setTimeZone(this.defaultTimezone)
  12. }
  13. /**
  14. * 设置时区
  15. * @param {Number} timezone 时区
  16. */
  17. setTimeZone(timezone) {
  18. if (timezone) {
  19. this.timezone = parseInt(timezone)
  20. }
  21. return this
  22. }
  23. /**
  24. * 获取 Date对象
  25. * @param {Date|Time} time
  26. */
  27. getDateObj(time) {
  28. return time ? new Date(time) : new Date()
  29. }
  30. /**
  31. * 获取毫秒/秒级时间戳
  32. * @param {DateTime} datetime 日期 例:'2022-04-21 00:00:00'
  33. * @param {Boolean} showSenconds 是否显示为秒级时间戳
  34. */
  35. getTime(datetime, showSenconds) {
  36. let time = this.getDateObj(datetime).getTime()
  37. if (showSenconds) {
  38. time = Math.trunc(time / 1000)
  39. }
  40. return time
  41. }
  42. /**
  43. * 获取日期
  44. * @param {String} dateFormat 日期格式
  45. * @param {Time} time 时间戳
  46. */
  47. getDate(dateFormat, time) {
  48. return this.dateFormat(dateFormat, time)
  49. }
  50. /**
  51. * 获取日期在不同时区下的时间戳
  52. * @param {Date|Time}} time 日期或时间戳
  53. * @param {Object} timezone 时区
  54. */
  55. getTimeByTimeZone(time, timezone) {
  56. this.setTimeZone(timezone)
  57. const thisDate = time ? new Date(time) : new Date()
  58. const localTime = thisDate.getTime()
  59. const offset = thisDate.getTimezoneOffset()
  60. const utc = offset * 60000 + localTime
  61. return utc + (3600000 * this.timezone)
  62. }
  63. /**
  64. * 获取时间信息
  65. * @param {Time} time 时间戳
  66. * @param {Boolean} full 是否完整展示, 为true时小于10的位会自动补0
  67. */
  68. getTimeInfo(time, full = true) {
  69. time = this.getTimeByTimeZone(time)
  70. const date = this.getDateObj(time)
  71. const retData = {
  72. nYear: date.getFullYear(),
  73. nMonth: date.getMonth() + 1,
  74. nWeek: date.getDay() || 7,
  75. nDay: date.getDate(),
  76. nHour: date.getHours(),
  77. nMinutes: date.getMinutes(),
  78. nSeconds: date.getSeconds()
  79. }
  80. if (full) {
  81. for (const k in retData) {
  82. if (retData[k] < 10) {
  83. retData[k] = '0' + retData[k]
  84. }
  85. }
  86. }
  87. return retData
  88. }
  89. /**
  90. * 时间格式转换
  91. * @param {String} format 展示格式如:Y-m-d H:i:s
  92. * @param {Time} time 时间戳
  93. */
  94. dateFormat(format, time) {
  95. const timeInfo = this.getTimeInfo(time)
  96. format = format || this.defaultDateFormat
  97. let date = format
  98. if (format) {
  99. date = date.replace(/Y/, timeInfo.nYear)
  100. }
  101. if (format.indexOf('m') !== false) {
  102. date = date.replace(/m/, timeInfo.nMonth)
  103. }
  104. if (format.indexOf('d') !== false) {
  105. date = date.replace(/d/, timeInfo.nDay)
  106. }
  107. if (format.indexOf('H') !== false) {
  108. date = date.replace(/H/, timeInfo.nHour)
  109. }
  110. if (format.indexOf('i') !== false) {
  111. date = date.replace(/i/, timeInfo.nMinutes)
  112. }
  113. if (format.indexOf('s') !== false) {
  114. date = date.replace(/s/, timeInfo.nSeconds)
  115. }
  116. return date
  117. }
  118. /**
  119. * 获取utc格式时间
  120. * @param {Date|Time} datetime 日期或时间戳
  121. */
  122. getUTC(datetime) {
  123. return this.getDateObj(datetime).toUTCString()
  124. }
  125. /**
  126. * 获取ISO 格式时间
  127. * @param {Date|Time} datetime 日期或时间戳
  128. */
  129. getISO(datetime) {
  130. return this.getDateObj(datetime).toISOString()
  131. }
  132. /**
  133. * 获取两时间相差天数
  134. * @param {Time} time1 时间戳
  135. * @param {Time} time2 时间戳
  136. */
  137. getDiffDays(time1, time2) {
  138. if (!time1) {
  139. return false
  140. }
  141. time2 = time2 ? time2 : this.getTime()
  142. let diffTime = time2 - time1
  143. if (diffTime < 0) {
  144. diffTime = Math.abs(diffTime)
  145. }
  146. return Math.ceil(diffTime / 86400000)
  147. }
  148. /**
  149. * 字符串转时间戳
  150. * @param {Object} str 字符串类型的时间戳
  151. */
  152. strToTime(str) {
  153. if (Array.from(str).length === 10) {
  154. str += '000'
  155. }
  156. return this.getTime(parseInt(str))
  157. }
  158. /**
  159. * 根据设置的天数获取指定日期N天后(前)的时间戳
  160. * @param {Number} days 天数
  161. * @param {Date|Time} time 指定的日期或时间戳
  162. * @param {Boolean} getAll 是否获取完整时间戳,为 false 时返回指定日期初始时间戳(当天00:00:00的时间戳)
  163. */
  164. getTimeBySetDays(days, time, getAll = false) {
  165. const date = this.getDateObj(time)
  166. date.setDate(date.getDate() + days)
  167. let startTime = date.getTime()
  168. if (!getAll) {
  169. const realdate = this.getDate('Y-m-d 00:00:00', startTime)
  170. startTime = this.getTimeByDateAndTimezone(realdate)
  171. }
  172. return startTime
  173. }
  174. /**
  175. * 根据设置的小时数获取指定日期N小时后(前)的时间戳
  176. * @param {Number} hours 小时数
  177. * @param {Date|Time} time 指定的日期或时间戳
  178. * @param {Boolean} getAll 是否获取完整时间戳,为 false 时返回指定时间初始时间戳(该小时00:00的时间戳)
  179. */
  180. getTimeBySetHours(hours, time, getAll = false) {
  181. const date = this.getDateObj(time)
  182. date.setHours(date.getHours() + hours)
  183. let startTime = date.getTime()
  184. if (!getAll) {
  185. const realdate = this.getDate('Y-m-d H:00:00', startTime)
  186. startTime = this.getTimeByDateAndTimezone(realdate)
  187. }
  188. return startTime
  189. }
  190. /**
  191. * 根据设置的周数获取指定日期N周后(前)的时间戳
  192. * @param {Number} weeks 周数
  193. * @param {Date|Time} time 指定的日期或时间戳
  194. * @param {Boolean} getAll 是否获取完整时间戳,为 false 时返回指定日期初始时间戳(当天00:00:00的时间戳)
  195. */
  196. getTimeBySetWeek(weeks, time, getAll = false) {
  197. const date = this.getDateObj(time)
  198. const dateInfo = this.getTimeInfo(time)
  199. const day = dateInfo.nWeek
  200. const offsetDays = 1 - day
  201. if (weeks) {
  202. weeks = weeks * 7 + offsetDays
  203. }
  204. date.setDate(date.getDate() + weeks)
  205. let startTime = date.getTime()
  206. if (!getAll) {
  207. const realdate = this.getDate('Y-m-d 00:00:00', startTime)
  208. startTime = this.getTimeByDateAndTimezone(realdate)
  209. }
  210. return startTime
  211. }
  212. /**
  213. * 根据设置的月数获取指定日期N月后(前)的时间戳
  214. * @param {Number} monthes 月数
  215. * @param {Date|Time} time 指定的日期或时间戳
  216. * @param {Boolean} getAll 是否获取完整时间戳,为 false 时返回指定日期初始时间戳(当天00:00:00的时间戳)
  217. */
  218. getTimeBySetMonth(monthes, time, getAll = false) {
  219. const date = this.getDateObj(time)
  220. date.setMonth(date.getMonth() + monthes)
  221. let startTime = date.getTime()
  222. if (!getAll) {
  223. const realdate = this.getDate('Y-m-01 00:00:00', startTime)
  224. startTime = this.getTimeByDateAndTimezone(realdate)
  225. }
  226. return startTime
  227. }
  228. /**
  229. * 根据时区获取指定时间的偏移时间
  230. * @param {Date|Time} 指定的日期或时间戳
  231. * @param {Number} timezone 时区
  232. */
  233. getTimeByDateAndTimezone(date, timezone) {
  234. if (!timezone) {
  235. timezone = this.timezone
  236. }
  237. const thisDate = this.getDateObj(date)
  238. const thisTime = thisDate.getTime()
  239. const offset = thisDate.getTimezoneOffset()
  240. const offsetTime = offset * 60000 + timezone * 3600000
  241. return thisTime - offsetTime
  242. }
  243. /**
  244. * 根据指定的时间类型获取时间范围
  245. * @param {String} type 时间类型 hour:小时 day:天 week:周 month:月
  246. * @param {Number} offset 时间的偏移量
  247. * @param {Date|Time} thistime 指定的日期或时间戳
  248. * @param {Boolean} getAll 是否获取完整时间戳,为 false 时返回指定日期初始时间戳(当天00:00:00的时间戳)
  249. */
  250. getTimeDimensionByType(type, offset = 0, thistime, getAll = false) {
  251. let startTime = 0
  252. let endTime = 0
  253. switch (type) {
  254. case 'hour': {
  255. startTime = this.getTimeBySetHours(offset, thistime, getAll)
  256. endTime = getAll ? startTime : startTime + 3599999
  257. break
  258. }
  259. case 'day': {
  260. startTime = this.getTimeBySetDays(offset, thistime, getAll)
  261. endTime = getAll ? startTime : startTime + 86399999
  262. break
  263. }
  264. case 'week': {
  265. startTime = this.getTimeBySetWeek(offset, thistime, getAll)
  266. endTime = getAll ? startTime + 86400000 * 6 : startTime + 86400000 * 6 + 86399999
  267. break
  268. }
  269. case 'month': {
  270. startTime = this.getTimeBySetMonth(offset, thistime, getAll)
  271. const date = this.getDateObj(this.getDate('Y-m-d H:i:s', startTime))
  272. const nextMonthFirstDayTime = new Date(date.getFullYear(), date.getMonth() + 1, 1).getTime()
  273. endTime = getAll ? nextMonthFirstDayTime - 86400000 : this.getTimeByDateAndTimezone(
  274. nextMonthFirstDayTime) - 1
  275. break
  276. }
  277. }
  278. return {
  279. startTime,
  280. endTime
  281. }
  282. }
  283. }