day.js 获取上月,当月,下月。day.js 获取年月日。获取一周、上周、本月、上月、本季度、上季度时间段。时间格式化 电脑版发表于:2023/8/21 10:50 官网:https://dayjs.fenxianglu.cn/ Element-plus中可以直接使用: ``` import { dayjs } from 'element-plus' ``` [TOC] ### day.js 时间格式化 ``` dayjs().format(); // 2020-09-08T13:42:32+08:00 dayjs().format('YYYY-MM-DD'); // 2020-09-08 dayjs().format('YYYY-MM-DD HH:mm:ss'); // 2020-09-08 13:47:12 dayjs(1318781876406).format('YYYY-MM-DD HH:mm:ss'); // 2011-10-17 00:17:56 dayjs(this.createDate).format('YYYY 年 MM 月 DD 日'); dayjs(this.createDate).format('YYYY年MM月DD日'); dayjs(this.createDate).format('YYYY/MM/DD'); ``` 传递从接口中来的数据: ``` dayjs(item.completeDateTime).format('YYYY-MM-DD HH:mm:ss') ``` ##### element table 表格时间格式化 ``` <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip> <template #default="scope"> <span>{{dayjs(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span> </template> </el-table-column> ``` ### day.js 获取上月,当月,下月 ``` onMounted(() => { // 获取下月 let nextMonth = dayjs().add(1, 'month').format('YYYY-MM') alert(nextMonth) // 当月 let nowMonth = dayjs().format('YYYY-MM'); alert(nowMonth) // 获取上月 let lastMonth = dayjs().add(-1, 'month').format('YYYY-MM') alert(lastMonth) getLevelList() }) ``` ### dayjs取 本周、上周、本月、上月、本季度、上季度时间段参考 ``` let dateTimes = [ { id: 1, name: '本周', start_time: dayjs().startOf('week').add(1, 'day').format('YYYY-MM-DD'), end_time: dayjs().endOf('week').add(1, 'day').format('YYYY-MM-DD'), }, { id: 2, name: '上周', start_time: dayjs().add(-1, 'week').startOf('week').add(1, 'day').format('YYYY-MM-DD'), end_time: dayjs().add(-1, 'week').endOf('week').add(1, 'day').format('YYYY-MM-DD'), }, { id: 3, name: '本月', start_time: dayjs().startOf('month').format('YYYY-MM-DD') , end_time: dayjs().endOf('month').format('YYYY-MM-DD'), }, { id: 4, name: '上月', start_time: dayjs().add(-1, 'month').startOf('month').format('YYYY-MM-DD') , end_time: dayjs().add(-1, 'month').endOf('month').format('YYYY-MM-DD'), }, ] let curMonth = dayjs().month() + 1 if (curMonth < 3) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(0).format('YYYY-MM-DD'), end_time: dayjs().month(2).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().add(-1, 'year').month(9).format('YYYY-MM-DD'), end_time: dayjs().add(-1, 'year').month(11).endOf('month').format('YYYY-MM-DD'), }, ) } else if (curMonth < 6) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(3).format('YYYY-MM-DD'), end_time: dayjs().month(5).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().month(0).format('YYYY-MM-DD'), end_time: dayjs().month(2).endOf('month').format('YYYY-MM-DD') , }, ) } else if (curMonth < 9) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(6).format('YYYY-MM-DD'), end_time: dayjs().month(8).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().month(3).format('YYYY-MM-DD'), end_time: dayjs().month(5).endOf('month').format('YYYY-MM-DD'), }, ) } else if (curMonth < 12) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(9).format('YYYY-MM-DD'), end_time: dayjs().month(11).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().month(6).format('YYYY-MM-DD'), end_time: dayjs().month(8).endOf('month').format('YYYY-MM-DD'), }, ) } ``` #### 这个有给element plus 日期组件el-date-picker增加快捷选择日期范围的 https://www.tnblog.net/xiuxin3/article/details/8644 ### day.js 获取年月日 ``` 年 : dayjs().year() 月 : dayjs().month() 日 : dayjs().date() 星期 : dayjs().day() 时 : dayjs().hour() 分 : dayjs().minute() 秒 : dayjs().second() 毫秒 : dayjs().millisecond() ```