/* * 若文档中已有命名dateFormat,可用dFormat()调用 * 年(Y) 可用1-4个占位符 * 月(m)、日(d)、小时(H)、分(M)、秒(S) 可用1-2个占位符 * 星期(W) 可用1-3个占位符 * 季度(q为阿拉伯数字,Q为中文数字)可用1或4个占位符 * * let date = new Date() * dateFormat("YYYY-mm-dd HH:MM:SS", date) 2020-02-09 14:04:23 * dateFormat("YYYY-mm-dd HH:MM:SS Q", date) 2020-02-09 14:09:03 一 * dateFormat("YYYY-mm-dd HH:MM:SS WWW", date) 2020-02-09 14:45:12 星期日 * dateFormat("YYYY-mm-dd HH:MM:SS QQQQ", date) 2020-02-09 14:09:36 第一季度 * dateFormat("YYYY-mm-dd HH:MM:SS WWW QQQQ", date) 2020-02-09 14:46:12 星期日 第一季度 */ export function dateFormat (format, date) { const we = date.getDay() // 星期 const qut = Math.floor((date.getMonth() + 3) / 3).toString() // 季度 const opt = { 'Y+': date.getFullYear().toString(), // 年 'm+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1) 'd+': date.getDate().toString(), // 日 'H+': date.getHours().toString(), // 时 'M+': date.getMinutes().toString(), // 分 'S+': date.getSeconds().toString(), // 秒 'q+': qut // 季度 } const week = { // 中文数字 (星期) 0: '日', 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六' } const quarter = { // 中文数字(季度) 1: '一', 2: '二', 3: '三', 4: '四' } if (/(W+)/.test(format)) { format = format.replace(RegExp.$1, (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we])) }; if (/(Q+)/.test(format)) { // 输入一个Q,只输出一个中文数字,输入4个Q,则拼接上字符串 format = format.replace(RegExp.$1, (RegExp.$1.length === 4 ? '第' + quarter[qut] + '季度' : quarter[qut])) }; for (const k in opt) { const r = new RegExp('(' + k + ')').exec(format) if (r) { // 若输入的长度不为1,则前面补零 format = format.replace(r[1], (RegExp.$1.length === 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0'))) } }; return format } /** * 获取当月第一天 * @returns {string} */ export function getMoonOneDay () { const date = new Date() const year = date.getFullYear().toString() // 年 const month = (date.getMonth()).toString() // 月(月份从0开始,要+1) return new Date(year, month, '1') } /** * 获取当月最后一天 * @returns {string} */ export function getMoonlastDay () { const date = new Date() const year = date.getFullYear().toString() // 年 const month = (date.getMonth() + 1).toString() // 月(月份从0开始,要+1) return new Date(year, month, 0) } export function getDatePickerTime (date) { const ret = [] if (!date) { return [-1, -1] } if (date[0]) { ret.push(date[0].getTime() / 1000) // 合作时间 } else { ret.push(-1) } if (date[1]) { ret.push(date[1].getTime() / 1000) // 合作时间 } else { ret.push(-1) } return ret } export function getDateYearSub (startDateStr, endDateStr) { const sDate = new Date(Date.parse(startDateStr.replace(/-/g, '/'))) let eDate = null if (!endDateStr) { eDate = new Date() } else { eDate = new Date(Date.parse(endDateStr.replace(/-/g, '/'))) } const diff = eDate.getFullYear() - sDate.getFullYear() return diff >= 0 ? diff : 0 } export function getNowSeconds () { const date = new Date() return date.getMilliseconds() } export function getYearFirstLastDay (year, q, needtime = true) { const y = !year ? new Date().getFullYear() : year let f = y + '-01-01' let l = y + '-12-31' if (needtime) { f = f + ' 00:00:00' l = l + ' 23:59:59' } if (q) { const timestr = (new Date(f)).valueOf() - 1000 const time = new Date(timestr) const year = time.getFullYear() const month = (time.getMonth() + 1).toString().padStart(2, '0') const date = (time.getDate()).toString().padStart(2, '0') const hours = (time.getHours()).toString().padStart(2, '0') const minute = (time.getMinutes()).toString().padStart(2, '0') const second = (time.getSeconds()).toString().padStart(2, '0') f = year + '-' + month + '-' + date if (needtime) { f = f + ' ' + hours + ':' + minute + ':' + second } } return [f, l, y.toString()] } // 获取现在的时间 年月日数组 export function getNow () { const date = new Date() return [ date.getFullYear().toString(), (date.getMonth() + 1).toString().padStart(2, '0'), (date.getDate()).toString().padStart(2, '0') ] } export function getYmFirstLastDay (ym, q) { let f = ym + '-01 00:00:00' const timestr = (new Date(f)).valueOf() - 1000 const time = new Date(timestr) const year = time.getFullYear() const month = (time.getMonth() + 1).toString().padStart(2, '0') const date = (time.getDate()).toString().padStart(2, '0') const hours = (time.getHours()).toString().padStart(2, '0') const minute = (time.getMinutes()).toString().padStart(2, '0') const second = (time.getSeconds()).toString().padStart(2, '0') f = year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second let e = (new Date(year, time.getMonth() + 2, 0)).valueOf() e = e + 86400000 - 1000 const time2 = new Date(e) const year2 = time2.getFullYear() const month2 = (time2.getMonth() + 1).toString().padStart(2, '0') const date2 = (time2.getDate()).toString().padStart(2, '0') const hours2 = (time2.getHours()).toString().padStart(2, '0') const minute2 = (time2.getMinutes()).toString().padStart(2, '0') const second2 = (time2.getSeconds()).toString().padStart(2, '0') e = year2 + '-' + month2 + '-' + date2 + ' ' + hours2 + ':' + minute2 + ':' + second2 return [f, e] } /** * 时间戳转时间 * @param {String} e * @param {Boolean} r * @returns */ export function timestampToTime (e, r) { e = r ? e : e * 1000 // console.log(e) const date = new Date(e) const Y = date.getFullYear() const M = (date.getMonth() + 1).toString().padStart(2, '0') const D = (date.getDate()).toString().padStart(2, '0') const h = (date.getHours()).toString().padStart(2, '0') const m = (date.getMinutes()).toString().padStart(2, '0') return Y + '-' + M + '-' + D + ' ' + h + ':' + m } export function getTenYear () { const yearArr = [] for (let i = new Date().getFullYear(); i > new Date().getFullYear() - 10; i--) { yearArr.push(i.toString()) } return yearArr } // 获取今年指定月份的前一个月 export function getLastMonthNowDate (month) { const date = new Date() const daysInMonth = [[0], [31], [28], [31], [30], [31], [30], [31], [31], [30], [31], [30], [31]] let strYear = date.getFullYear() let strMonth = month if (strYear % 4 === 0 && strYear % 100 !== 0) { daysInMonth[2] = 29 } if (strMonth - 1 === 0) { strYear -= 1 strMonth = 12 } else { strMonth -= 1 } if (strMonth < 10) { strMonth = '0' + strMonth } const dateStr = strYear + '-' + strMonth return dateStr } export function getTimeStamp (timeStamp) { const inDate = new Date(timeStamp) const year = inDate.getFullYear() const month = inDate.getMonth() const date = new Date() const currentMonth = date.getMonth() const startTime = new Date(year, month, 1).getTime() let endTime = '' if (month === currentMonth) { endTime = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1 // 当天23:59:59时间戳 } else { endTime = new Date(year, month + 1, 1).getTime() - 1 } return { startTime: startTime, endTime: endTime } } // 获取当前月最后一天 export function getCurrentMonthLast () { const date = new Date() let currentMonth = date.getMonth() const nextMonth = ++currentMonth const nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1) const oneDay = 1000 * 60 * 60 * 24 const lastTime = new Date(nextMonthFirstDay - oneDay) let month = parseInt(lastTime.getMonth() + 1) let day = lastTime.getDate() if (month < 10) { month = '0' + month } if (day < 10) { day = '0' + day } return date.getFullYear() + '-' + month + '-' + day } // 获取当月第一天 export function getCurrentMonthFirst () { const date = new Date() date.setDate(1) let month = parseInt(date.getMonth() + 1) let day = date.getDate() if (month < 10) { month = '0' + month } if (day < 10) { day = '0' + day } return date.getFullYear() + '-' + month + '-' + day } // 获取某个时间段内的所有年月 export function getDateArry (startDate, endDate) { const d1 = startDate const d2 = endDate const dateArry = [] const s1 = d1.split('-') const s2 = d2.split('-') let mCount = 0 if (parseInt(s1[0]) < parseInt(s2[0])) { mCount = (parseInt(s2[0]) - parseInt(s1[0])) * 12 + parseInt(s2[1]) - parseInt(s1[1]) + 1 } else { mCount = parseInt(s2[1]) - parseInt(s1[1]) + 1 } if (mCount > 0) { let startM = parseInt(s1[1]) let startY = parseInt(s1[0]) for (let i = 0; i < mCount; i++) { if (startM < 12) { dateArry[i] = startY + '-' + (startM > 9 ? startM : '0' + startM) startM += 1 } else { dateArry[i] = startY + '-' + (startM > 9 ? startM : '0' + startM) startM = 1 startY += 1 } } } return dateArry } // getDateArry('2022-01-01','2022-12-31') // getDateArry('2022-01','2022-12') // 获取上个月第一天和最后一天 export function lastMonthFirstDayLastDay () { const nowdays = new Date() let year = nowdays.getFullYear() let month = nowdays.getMonth() if (month === 0) { month = 12 year = year - 1 } if (month < 10) { month = '0' + month } const myDate = new Date(year, month, 0) const startDate = year + '-' + month + '-01' // 上个月第一天 const endDate = year + '-' + month + '-' + myDate.getDate()// 上个月最后一天 return [startDate, endDate] } // 获取指定月份的第一天和最后一天 export function monthFirstDayLastDay (value) { if (!value) return ['', ''] // value:'2023-12'; date.getDate(): 第几天(从 1 到 31) const date = new Date(value) const year = date.getFullYear() let month = date.getMonth() + 1 const myDate = new Date(year, month, 0) if (month < 10) { month = '0' + month } const startDate = year + '-' + month + '-01' // 第一天 const endDate = year + '-' + month + '-' + myDate.getDate()// 最后一天 return [startDate, endDate] } // 获取指定月份的最后一天 export function MonthLastDay (endDate) { const date = new Date(endDate) const time1 = date.getTime() const start = getTimeStamp(time1) const end = new Date(start.endTime) const y = end.getFullYear() let M = end.getMonth() + 1 let d if (M < 10) { M = `0${M}` } if (M === 11) { d = 30 } else { d = end.getDate() } const newend = `${y}-${M}-${d}` return newend } // MonthLastDay('2022-05') // 用于计算当前日期推的前年 // 日期计算 export function getDay (d, num) { const date = new Date(new Date(d).setDate(new Date(d).getDate() + num)) const year = date.getFullYear() const month = (date.getMonth() + 1 + '').padStart(2, '0') const day = (date.getDate() + '').padStart(2, '0') return `${year}-${month}-${day}` } // 是否是闰年 export function isLeap (year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 } // 返回前年日期 export function last2YearDate (d) { const date = new Date(d) const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const last2Year = year - 2 const lastYear = year - 1 const lastYearIsLeap = isLeap(lastYear) const thisYearisLeap = isLeap(lastYear) const last2YearIsLeap = isLeap(last2Year) if (last2YearIsLeap || lastYearIsLeap || (thisYearisLeap && month === 12 && day === 31)) return getDay(d, -366 - 365) return getDay(d, -365 * 2) } // 时间戳转换 export function currentTime (timestamp) { const date = new Date(timestamp) const Y = date.getFullYear() + '-' const M = (date.getMonth() + 1).toString().padStart(2, '0') + '-' const D = date.getDate().toString().padStart(2, '0') + ' ' const h = date.getHours().toString().padStart(2, '0') + ':' const m = date.getMinutes().toString().padStart(2, '0') + ':' const s = date.getSeconds().toString().padStart(2, '0') const strDate = Y + M + D + h + m + s return strDate } /** * 获取当前时分秒 * @param {Number} overflow 溢出秒数 * @returns */ export function currentHMS (overflow = 0) { const date = new Date() // const overSeconds = +date.getSeconds() + overflow let h = +date.getHours() let m = +date.getMinutes() let s = +date.getSeconds() + overflow if (s > 59) { s = s - 60 m++ } if (m > 59) { m = m - 60 h++ } if (h > 23) { h = 0 } h = h.toString().padStart(2, '0') m = m.toString().padStart(2, '0') s = s.toString().padStart(2, '0') return `${h}:${m}:${s}` } export function dealMonths (startMonth, endMonth) { const newstart = startMonth + '-01' const one = endMonth.slice(0, 4) const two = endMonth.slice(5, 7) const newend = endMonth + '-' + new Date(one, two, 0).getDate() return [newstart, newend] } // Mon, 19 Feb 2024 17:05:00 GMT转换为YY-MM-DD格式 export function formatGMT (time) { const date = new Date(time) const m = (date.getMonth() + 1) // const mm = '-' + (m < 10 ? '0' + m : m) const d = date.getDate() // const dd = '-' + (d < 10 ? '0' + d : d) return date.getFullYear() + '-' + m.toString().padStart(2, '0') + '-' + d.toString().padStart(2, '0') }