代码示例:
initDate () {
var end = new Date()
var year = end.getFullYear()
var month = end.getMonth() + 1
var day = end.getDate()
var dateObj = {}
dateObj.end = `${year}-${month}-${day}`
var endMonthDay = new Date(year, month, 0).getDate() // 当前月的总天数
if (month - 1 <= 0) { // 如果是1月,年数往前推一年
dateObj.start = (year - 1) + '-' + 12 + '-' + (day - 1)
} else {
var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate()
if (startMonthDay < day) { // 1个月前所在月的总天数小于现在的天日期
if (day < endMonthDay) { // 当前天日期小于当前月总天数
dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day) - 1)
} else {
dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - 1)
}
} else {
dateObj.start = year + '-' + (month - 1) + '-' + (day - 1)
}
}
this.timeValue = [`${dateObj.start} 00:00:00`, `${dateObj.end} 23:59:59`]
}
效果:
image.png
网友评论