Date 对象 整理一些时间格式化的方法 1.
function format_date (timeStamp ) { let date = new Date (timeStamp) return date.getFullYear() + '年' + prefix_zero(date.getMonth() + 1 ) + '月' + prefix_zero(date.getDate()) + '日 ' + prefix_zero(date.getHours()) + ':' + prefix_zero(date.getMinutes()) }
function prefix_zero (num ) { return num >= 10 ? num : '0' + num }
2.
function getYesterday ( ) { var d = new Date () d.setDate(d.getDate() - 1 ) return d.toLocaleString() }
3.
const getTomorrow = () => { let d = new Date () d.setDate(d.getDate() + 1 ) return d.toLocalString() }
4.
function getWeekDate (start = 0 ) { let d = new Date ('2019-12-15' ) let D = d.getDate() if (start == 0 ) { return d.setDate(D - d.getDay()) } else { let W = d.getDay() || 6 return d.setDate(D - W + 1 ) } }
5.
function getLastWeek ( ) { let d = new Date () let D = d.getDate() d.setDate(D - 7 ) return [d, new Date ()] }
6.
function getMonthDays (year, month ) { let d = new Date () year = year || d.getFullYear() month = month || d.getMonth() var monthStart = new Date (year, month, 1 ) var monthEnd = new Date (year, month + 1 , 1 ) var days = (monthEnd - monthStart) / (1000 * 60 * 60 * 24 ) return days }
7.
function getMonthDate (isEnd = 0 ) { let d = new Date () d.setDate(1 ) if (isEnd) { d.setDate(d.getDate() + getMonthDays() - 1 ) return d } return d }
8.
function format_time (timeStamp ) { let day = Math .floor(timeStamp / (24 * 3600 * 1000 )) let leave1 = timeStamp % (24 * 3600 * 1000 ) let hours = Math .floor(leave1 / (3600 * 1000 )) let leave2 = leave1 % (3600 * 1000 ) let minutes = Math .floor(leave2 / (60 * 1000 )) let leave3 = leave2 % (60 * 1000 ) let seconds = Math .floor(leave3 / 1000 ) if (day) return day + '天' + hours + '小时' + minutes + '分' if (hours) return hours + '小时' + minutes + '分' + seconds + '秒' if (minutes) return minutes + '分' + seconds + '秒' if (seconds) return seconds + '秒' return '时间到!' }
dayjs 库 轻量的处理时间和日期的 JavaScript 库
参考文档 github 中文文档
在本页面打开控制台即可体验 dayjs 的 api
使用 dayjs 获取本周,上周,本月,上月,本季度,上季度,本年,去年时间
let date = dayjs()let day = date.day() let currentWeekStartlet currentWeekEndif (day > 0 ) { currentWeekStart = date.startOf('week' ).add(1 , 'day' ) } else { currentWeekStart = date.startOf('week' ).subtract(6 , 'day' ) } currentWeekEnd = currentWeekStart.add(6 , 'day' ) let lastWeekStart = currentWeekStart.subtract(7 , 'day' )let lastWeekEnd = currentWeekStart.subtract(1 , 'day' )let currentMonthStart = date.date(1 )let lastMonthStart = date.subtract(1 , 'month' ).date(1 )let nextMonthStart = date.add(1 , 'month' ).date(1 )let currentMonthEnd = nextMonthStart.subtract(1 , 'day' )let lastMonthEnd = currentMonthStart.subtract(1 , 'day' )let currentYearStart = date.month(0 ).date(1 )let currentYearEnd = date.month(11 ).date(31 )let lastYear = date.subtract(1 , 'year' )let lastYearStart = date.subtract(1 , 'year' ).month(0 ).date(1 )let lastYearEnd = lastYearStart.month(11 ).date(31 )let computedDate = { currentWeekStart: currentWeekStart.format('YYYY-MM-DD' ), currentWeekEnd: currentWeekEnd.format('YYYY-MM-DD' ), lastWeekStart: lastWeekStart.format('YYYY-MM-DD' ), lastWeekEnd: lastWeekEnd.format('YYYY-MM-DD' ), currentMonthStart: currentMonthStart.format('YYYY-MM-DD' ), currentMonthEnd: currentMonthEnd.format('YYYY-MM-DD' ), lastMonthStart: lastMonthStart.format('YYYY-MM-DD' ), lastMonthEnd: lastMonthEnd.format('YYYY-MM-DD' ), currentYearStart: currentYearStart.format('YYYY-MM-DD' ), currentYearEnd: currentYearEnd.format('YYYY-MM-DD' ), lastYearStart: lastYearStart.format('YYYY-MM-DD' ), lastYearEnd: lastYearEnd.format('YYYY-MM-DD' ) } console .log(computedDate)