date.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * 若文档中已有命名dateFormat,可用dFormat()调用
  3. * 年(Y) 可用1-4个占位符
  4. * 月(m)、日(d)、小时(H)、分(M)、秒(S) 可用1-2个占位符
  5. * 星期(W) 可用1-3个占位符
  6. * 季度(q为阿拉伯数字,Q为中文数字)可用1或4个占位符
  7. *
  8. * let date = new Date()
  9. * dateFormat("YYYY-mm-dd HH:MM:SS", date) 2020-02-09 14:04:23
  10. * dateFormat("YYYY-mm-dd HH:MM:SS Q", date) 2020-02-09 14:09:03 一
  11. * dateFormat("YYYY-mm-dd HH:MM:SS WWW", date) 2020-02-09 14:45:12 星期日
  12. * dateFormat("YYYY-mm-dd HH:MM:SS QQQQ", date) 2020-02-09 14:09:36 第一季度
  13. * dateFormat("YYYY-mm-dd HH:MM:SS WWW QQQQ", date) 2020-02-09 14:46:12 星期日 第一季度
  14. */
  15. export function dateFormat (format, date) {
  16. const we = date.getDay() // 星期
  17. const qut = Math.floor((date.getMonth() + 3) / 3).toString() // 季度
  18. const opt = {
  19. 'Y+': date.getFullYear().toString(), // 年
  20. 'm+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1)
  21. 'd+': date.getDate().toString(), // 日
  22. 'H+': date.getHours().toString(), // 时
  23. 'M+': date.getMinutes().toString(), // 分
  24. 'S+': date.getSeconds().toString(), // 秒
  25. 'q+': qut // 季度
  26. }
  27. const week = { // 中文数字 (星期)
  28. 0: '日',
  29. 1: '一',
  30. 2: '二',
  31. 3: '三',
  32. 4: '四',
  33. 5: '五',
  34. 6: '六'
  35. }
  36. const quarter = { // 中文数字(季度)
  37. 1: '一',
  38. 2: '二',
  39. 3: '三',
  40. 4: '四'
  41. }
  42. if (/(W+)/.test(format)) {
  43. format = format.replace(RegExp.$1, (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we]))
  44. };
  45. if (/(Q+)/.test(format)) {
  46. // 输入一个Q,只输出一个中文数字,输入4个Q,则拼接上字符串
  47. format = format.replace(RegExp.$1, (RegExp.$1.length === 4 ? '第' + quarter[qut] + '季度' : quarter[qut]))
  48. };
  49. for (const k in opt) {
  50. const r = new RegExp('(' + k + ')').exec(format)
  51. if (r) {
  52. // 若输入的长度不为1,则前面补零
  53. format = format.replace(r[1], (RegExp.$1.length === 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0')))
  54. }
  55. };
  56. return format
  57. }
  58. /**
  59. * 获取当月第一天
  60. * @returns {string}
  61. */
  62. export function getMoonOneDay () {
  63. const date = new Date()
  64. const year = date.getFullYear().toString() // 年
  65. const month = (date.getMonth()).toString() // 月(月份从0开始,要+1)
  66. return new Date(year, month, '1')
  67. }
  68. /**
  69. * 获取当月最后一天
  70. * @returns {string}
  71. */
  72. export function getMoonlastDay () {
  73. const date = new Date()
  74. const year = date.getFullYear().toString() // 年
  75. const month = (date.getMonth() + 1).toString() // 月(月份从0开始,要+1)
  76. return new Date(year, month, 0)
  77. }
  78. export function getDatePickerTime (date) {
  79. const ret = []
  80. if (!date) {
  81. return [-1, -1]
  82. }
  83. if (date[0]) {
  84. ret.push(date[0].getTime() / 1000) // 合作时间
  85. } else {
  86. ret.push(-1)
  87. }
  88. if (date[1]) {
  89. ret.push(date[1].getTime() / 1000) // 合作时间
  90. } else {
  91. ret.push(-1)
  92. }
  93. return ret
  94. }
  95. export function getDateYearSub (startDateStr, endDateStr) {
  96. const sDate = new Date(Date.parse(startDateStr.replace(/-/g, '/')))
  97. let eDate = null
  98. if (!endDateStr) {
  99. eDate = new Date()
  100. } else {
  101. eDate = new Date(Date.parse(endDateStr.replace(/-/g, '/')))
  102. }
  103. const diff = eDate.getFullYear() - sDate.getFullYear()
  104. return diff >= 0 ? diff : 0
  105. }
  106. export function getNowSeconds () {
  107. const date = new Date()
  108. return date.getMilliseconds()
  109. }
  110. export function getYearFirstLastDay (year, q, needtime = true) {
  111. const y = !year ? new Date().getFullYear() : year
  112. let f = y + '-01-01'
  113. let l = y + '-12-31'
  114. if (needtime) {
  115. f = f + ' 00:00:00'
  116. l = l + ' 23:59:59'
  117. }
  118. if (q) {
  119. const timestr = (new Date(f)).valueOf() - 1000
  120. const time = new Date(timestr)
  121. const year = time.getFullYear()
  122. const month = (time.getMonth() + 1).toString().padStart(2, '0')
  123. const date = (time.getDate()).toString().padStart(2, '0')
  124. const hours = (time.getHours()).toString().padStart(2, '0')
  125. const minute = (time.getMinutes()).toString().padStart(2, '0')
  126. const second = (time.getSeconds()).toString().padStart(2, '0')
  127. f = year + '-' + month + '-' + date
  128. if (needtime) {
  129. f = f + ' ' + hours + ':' + minute + ':' + second
  130. }
  131. }
  132. return [f, l, y.toString()]
  133. }
  134. // 获取现在的时间 年月日数组
  135. export function getNow () {
  136. const date = new Date()
  137. return [
  138. date.getFullYear().toString(),
  139. (date.getMonth() + 1).toString().padStart(2, '0'),
  140. (date.getDate()).toString().padStart(2, '0')
  141. ]
  142. }
  143. export function getYmFirstLastDay (ym, q) {
  144. let f = ym + '-01 00:00:00'
  145. const timestr = (new Date(f)).valueOf() - 1000
  146. const time = new Date(timestr)
  147. const year = time.getFullYear()
  148. const month = (time.getMonth() + 1).toString().padStart(2, '0')
  149. const date = (time.getDate()).toString().padStart(2, '0')
  150. const hours = (time.getHours()).toString().padStart(2, '0')
  151. const minute = (time.getMinutes()).toString().padStart(2, '0')
  152. const second = (time.getSeconds()).toString().padStart(2, '0')
  153. f = year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
  154. let e = (new Date(year, time.getMonth() + 2, 0)).valueOf()
  155. e = e + 86400000 - 1000
  156. const time2 = new Date(e)
  157. const year2 = time2.getFullYear()
  158. const month2 = (time2.getMonth() + 1).toString().padStart(2, '0')
  159. const date2 = (time2.getDate()).toString().padStart(2, '0')
  160. const hours2 = (time2.getHours()).toString().padStart(2, '0')
  161. const minute2 = (time2.getMinutes()).toString().padStart(2, '0')
  162. const second2 = (time2.getSeconds()).toString().padStart(2, '0')
  163. e = year2 + '-' + month2 + '-' + date2 + ' ' + hours2 + ':' + minute2 + ':' + second2
  164. return [f, e]
  165. }
  166. /**
  167. * 时间戳转时间
  168. * @param {String} e
  169. * @param {Boolean} r
  170. * @returns
  171. */
  172. export function timestampToTime (e, r) {
  173. e = r ? e : e * 1000
  174. // console.log(e)
  175. const date = new Date(e)
  176. const Y = date.getFullYear()
  177. const M = (date.getMonth() + 1).toString().padStart(2, '0')
  178. const D = (date.getDate()).toString().padStart(2, '0')
  179. const h = (date.getHours()).toString().padStart(2, '0')
  180. const m = (date.getMinutes()).toString().padStart(2, '0')
  181. return Y + '-' + M + '-' + D + ' ' + h + ':' + m
  182. }
  183. export function getTenYear () {
  184. const yearArr = []
  185. for (let i = new Date().getFullYear(); i > new Date().getFullYear() - 10; i--) {
  186. yearArr.push(i.toString())
  187. }
  188. return yearArr
  189. }
  190. // 获取今年指定月份的前一个月
  191. export function getLastMonthNowDate (month) {
  192. const date = new Date()
  193. const daysInMonth = [[0], [31], [28], [31], [30], [31], [30], [31], [31], [30], [31], [30], [31]]
  194. let strYear = date.getFullYear()
  195. let strMonth = month
  196. if (strYear % 4 === 0 && strYear % 100 !== 0) {
  197. daysInMonth[2] = 29
  198. }
  199. if (strMonth - 1 === 0) {
  200. strYear -= 1
  201. strMonth = 12
  202. } else {
  203. strMonth -= 1
  204. }
  205. if (strMonth < 10) {
  206. strMonth = '0' + strMonth
  207. }
  208. const dateStr = strYear + '-' + strMonth
  209. return dateStr
  210. }
  211. export function getTimeStamp (timeStamp) {
  212. const inDate = new Date(timeStamp)
  213. const year = inDate.getFullYear()
  214. const month = inDate.getMonth()
  215. const date = new Date()
  216. const currentMonth = date.getMonth()
  217. const startTime = new Date(year, month, 1).getTime()
  218. let endTime = ''
  219. if (month === currentMonth) {
  220. endTime =
  221. new Date(new Date().toLocaleDateString()).getTime() +
  222. 24 * 60 * 60 * 1000 - 1 // 当天23:59:59时间戳
  223. } else {
  224. endTime = new Date(year, month + 1, 1).getTime() - 1
  225. }
  226. return {
  227. startTime: startTime,
  228. endTime: endTime
  229. }
  230. }
  231. // 获取当前月最后一天
  232. export function getCurrentMonthLast () {
  233. const date = new Date()
  234. let currentMonth = date.getMonth()
  235. const nextMonth = ++currentMonth
  236. const nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
  237. const oneDay = 1000 * 60 * 60 * 24
  238. const lastTime = new Date(nextMonthFirstDay - oneDay)
  239. let month = parseInt(lastTime.getMonth() + 1)
  240. let day = lastTime.getDate()
  241. if (month < 10) {
  242. month = '0' + month
  243. }
  244. if (day < 10) {
  245. day = '0' + day
  246. }
  247. return date.getFullYear() + '-' + month + '-' + day
  248. }
  249. // 获取当月第一天
  250. export function getCurrentMonthFirst () {
  251. const date = new Date()
  252. date.setDate(1)
  253. let month = parseInt(date.getMonth() + 1)
  254. let day = date.getDate()
  255. if (month < 10) {
  256. month = '0' + month
  257. }
  258. if (day < 10) {
  259. day = '0' + day
  260. }
  261. return date.getFullYear() + '-' + month + '-' + day
  262. }
  263. // 获取某个时间段内的所有年月
  264. export function getDateArry (startDate, endDate) {
  265. const d1 = startDate
  266. const d2 = endDate
  267. const dateArry = []
  268. const s1 = d1.split('-')
  269. const s2 = d2.split('-')
  270. let mCount = 0
  271. if (parseInt(s1[0]) < parseInt(s2[0])) {
  272. mCount = (parseInt(s2[0]) - parseInt(s1[0])) * 12 + parseInt(s2[1]) - parseInt(s1[1]) + 1
  273. } else {
  274. mCount = parseInt(s2[1]) - parseInt(s1[1]) + 1
  275. }
  276. if (mCount > 0) {
  277. let startM = parseInt(s1[1])
  278. let startY = parseInt(s1[0])
  279. for (let i = 0; i < mCount; i++) {
  280. if (startM < 12) {
  281. dateArry[i] = startY + '-' + (startM > 9 ? startM : '0' + startM)
  282. startM += 1
  283. } else {
  284. dateArry[i] = startY + '-' + (startM > 9 ? startM : '0' + startM)
  285. startM = 1
  286. startY += 1
  287. }
  288. }
  289. }
  290. return dateArry
  291. }
  292. // getDateArry('2022-01-01','2022-12-31')
  293. // getDateArry('2022-01','2022-12')
  294. // 获取上个月第一天和最后一天
  295. export function lastMonthFirstDayLastDay () {
  296. const nowdays = new Date()
  297. let year = nowdays.getFullYear()
  298. let month = nowdays.getMonth()
  299. if (month === 0) {
  300. month = 12
  301. year = year - 1
  302. }
  303. if (month < 10) {
  304. month = '0' + month
  305. }
  306. const myDate = new Date(year, month, 0)
  307. const startDate = year + '-' + month + '-01' // 上个月第一天
  308. const endDate = year + '-' + month + '-' + myDate.getDate()// 上个月最后一天
  309. return [startDate, endDate]
  310. }
  311. // 获取指定月份的第一天和最后一天
  312. export function monthFirstDayLastDay (value) {
  313. if (!value) return ['', '']
  314. // value:'2023-12'; date.getDate(): 第几天(从 1 到 31)
  315. const date = new Date(value)
  316. const year = date.getFullYear()
  317. let month = date.getMonth() + 1
  318. const myDate = new Date(year, month, 0)
  319. if (month < 10) {
  320. month = '0' + month
  321. }
  322. const startDate = year + '-' + month + '-01' // 第一天
  323. const endDate = year + '-' + month + '-' + myDate.getDate()// 最后一天
  324. return [startDate, endDate]
  325. }
  326. // 获取指定月份的最后一天
  327. export function MonthLastDay (endDate) {
  328. const date = new Date(endDate)
  329. const time1 = date.getTime()
  330. const start = getTimeStamp(time1)
  331. const end = new Date(start.endTime)
  332. const y = end.getFullYear()
  333. let M = end.getMonth() + 1
  334. let d
  335. if (M < 10) {
  336. M = `0${M}`
  337. }
  338. if (M === 11) {
  339. d = 30
  340. } else {
  341. d = end.getDate()
  342. }
  343. const newend = `${y}-${M}-${d}`
  344. return newend
  345. }
  346. // MonthLastDay('2022-05')
  347. // 用于计算当前日期推的前年
  348. // 日期计算
  349. export function getDay (d, num) {
  350. const date = new Date(new Date(d).setDate(new Date(d).getDate() + num))
  351. const year = date.getFullYear()
  352. const month = (date.getMonth() + 1 + '').padStart(2, '0')
  353. const day = (date.getDate() + '').padStart(2, '0')
  354. return `${year}-${month}-${day}`
  355. }
  356. // 是否是闰年
  357. export function isLeap (year) {
  358. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
  359. }
  360. // 返回前年日期
  361. export function last2YearDate (d) {
  362. const date = new Date(d)
  363. const year = date.getFullYear()
  364. const month = date.getMonth() + 1
  365. const day = date.getDate()
  366. const last2Year = year - 2
  367. const lastYear = year - 1
  368. const lastYearIsLeap = isLeap(lastYear)
  369. const thisYearisLeap = isLeap(lastYear)
  370. const last2YearIsLeap = isLeap(last2Year)
  371. if (last2YearIsLeap || lastYearIsLeap || (thisYearisLeap && month === 12 && day === 31)) return getDay(d, -366 - 365)
  372. return getDay(d, -365 * 2)
  373. }
  374. // 时间戳转换
  375. export function currentTime (timestamp) {
  376. const date = new Date(timestamp)
  377. const Y = date.getFullYear() + '-'
  378. const M = (date.getMonth() + 1).toString().padStart(2, '0') + '-'
  379. const D = date.getDate().toString().padStart(2, '0') + ' '
  380. const h = date.getHours().toString().padStart(2, '0') + ':'
  381. const m = date.getMinutes().toString().padStart(2, '0') + ':'
  382. const s = date.getSeconds().toString().padStart(2, '0')
  383. const strDate = Y + M + D + h + m + s
  384. return strDate
  385. }
  386. /**
  387. * 获取当前时分秒
  388. * @param {Number} overflow 溢出秒数
  389. * @returns
  390. */
  391. export function currentHMS (overflow = 0) {
  392. const date = new Date()
  393. // const overSeconds = +date.getSeconds() + overflow
  394. let h = +date.getHours()
  395. let m = +date.getMinutes()
  396. let s = +date.getSeconds() + overflow
  397. if (s > 59) {
  398. s = s - 60
  399. m++
  400. }
  401. if (m > 59) {
  402. m = m - 60
  403. h++
  404. }
  405. if (h > 23) {
  406. h = 0
  407. }
  408. h = h.toString().padStart(2, '0')
  409. m = m.toString().padStart(2, '0')
  410. s = s.toString().padStart(2, '0')
  411. return `${h}:${m}:${s}`
  412. }
  413. export function dealMonths (startMonth, endMonth) {
  414. const newstart = startMonth + '-01'
  415. const one = endMonth.slice(0, 4)
  416. const two = endMonth.slice(5, 7)
  417. const newend = endMonth + '-' + new Date(one, two, 0).getDate()
  418. return [newstart, newend]
  419. }
  420. // Mon, 19 Feb 2024 17:05:00 GMT转换为YY-MM-DD格式
  421. export function formatGMT (time) {
  422. const date = new Date(time)
  423. const m = (date.getMonth() + 1)
  424. // const mm = '-' + (m < 10 ? '0' + m : m)
  425. const d = date.getDate()
  426. // const dd = '-' + (d < 10 ? '0' + d : d)
  427. return date.getFullYear() + '-' + m.toString().padStart(2, '0') + '-' + d.toString().padStart(2, '0')
  428. }