时间相关方法

mac2025-06-05  66

// 当前年份 const FullYear=()=>{ var myDate = new Date(); return myDate.getFullYear(); } //2017-01-01 转成 时间戳 123123123412 function changeTime(time) { return Date.parse(new Date(time)); } // 后端返回时间插件格式化 function postForMatDate(date) { let reg = /[-]/g; let reg1 = /[~]/g; let tiemDate1 = date; tiemDate1 = tiemDate1.replace(reg, '/'); tiemDate1 = tiemDate1.replace(reg1, '-'); return tiemDate1 } // 时间插件格式化 function getForMatDate(date) { let reg = /[/]/g; let reg1 = /[-]/g; let tiemDate1 = date; tiemDate1 = tiemDate1.replace(reg1, '~'); tiemDate1 = tiemDate1.replace(reg, '-'); return tiemDate1 } // 时间戳转时间段 function toDate(n, m) { //如果是毫秒的时间戳就不需要这一步,直接下一步就可以 var date = new Date(n); var Y = date.getFullYear() + ''; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + ''; var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); var date2 = new Date(m); var Y2 = date2.getFullYear() + ''; var M2 = (date2.getMonth() + 1 < 10 ? '0' + (date2.getMonth() + 1) : date2.getMonth() + 1) + ''; var D2 = date2.getDate() < 10 ? '0' + date2.getDate() : date2.getDate(); return (Y + M + D + " - " + Y2 + M2 + D2); } function dataToString(n, m) { var date1 = new Date(n); var data2 = new Date(m); function t(date) { var Y = date.getFullYear() + '-'; var M = date.getMonth() + 1 < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1) + '-'; var D = date.getDate() < 10 ? ('0' + date.getDate() ) : data2.getDate(); return Y + M + D; } return t(date1) + '~' + t(data2) } // 从今日向后推一周 // 从今日向后推七周 function gethebdomad(){ const end = new Date(); const start = new Date(); end.setHours(23, 59, 59); return start.setTime(end.getTime() - 3600 * 1000 * 24 * 7 + 1000); } function hepta(){ const end = new Date(); const start = new Date(); end.setHours(23, 59, 59); return start.setTime(end.getTime() - 3600 * 1000 * 24 * 49 + 1000); } // 时间戳转时间段 function newdate(m) { var date2 = new Date(m); var Y2 = date2.getFullYear() + ''; var M2 = (date2.getMonth() + 1 < 10 ? '0' + (date2.getMonth() + 1) : date2.getMonth() + 1) + ''; var D2 = date2.getDate() < 10 ? '0' + date2.getDate() : date2.getDate(); return (Y2 + M2 + D2); } 获取每月有几周 // year:年 month:月 day:日 getWeeks(year, month, day) { const d = new Date() // 该月第一天 d.setFullYear(2018, 6, 1) let w1 = d.getDay() if (w1 === 0) { w1 = 7 } // 该月天数 d.setFullYear(2018, 7, 0) const dd = d.getDate() // 该月第一个周一 let d1 if (w1 !== 1) { d1 = 7 - w1 + 2 } else { d1 = 1 } const WEEK_NUB = Math.ceil((dd - d1 + 1) / 7) return WEEK_NUB } 获得周期名字 getWeekName() { const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] const index = new Date().getDay() const currDay = weekday[index] return currDay } 获得当前日期在当月第几周 // a: 年 b: 月 c: 日 (不包括跟上个月重合的部分) getMonthWeek(a, b, c) { const date = new Date(a, parseInt(b) - 1, c) const w = date.getDay() const d = date.getDate() return Math.ceil( (d + 6 - w) / 7 ) }
最新回复(0)