vue时间转换-将时间转换为本月第几周-方法记录
1、引入moment时间转换
const moment = require('moment')
2、获取当前时间
var key='周报'
var dateTime='2021-06-01 15:14:15'
this.checkDate (key, dateTime)
3。转换
checkDate (key, dateTime) { if (key && dateTime) { if (key == '日报') { return moment(dateTime).format('YYYY年MM月DD日') } if (key == '月报') { return moment(dateTime).format('YYYY年MM月') } if (key == '周报') { var yearNum = moment(dateTime).format('YYYY') var monthNum = moment(dateTime).format('MM') var dayNum = moment(dateTime).format('DD') var getDate = this.getMonthWeek(yearNum, monthNum, dayNum) var dateStr = getDate.getYear + '年' + getDate.getMonth + '月第' + getDate.getWeek + '周' return dateStr } } else { return '-' } }, getMonthWeek (yearNum, monthNum, dayNum) { var date = new Date(yearNum, parseInt(monthNum) - 1, dayNum) var w = date.getDay() var d = date.getDate() if (w == 0) { w = 7 } var config = { getMonth: date.getMonth() + 1, getYear: date.getFullYear(), getWeek: Math.ceil((d + 6 - w) / 7) } return config }