比如现在日期是2021-04,隔两个月就是4月加两月等于6月,
我要6月的第二周的星期三的日期。例如:2021-06-09
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btn">1111111111111111</button>
<script>;
var params = {
mouth:3,
num:1,
week:1
}
let getDate = function(data){
let now = new Date() //获取当前时间
let year = now.getFullYear() //获取当前年
let mouth = now.getMonth() //获取当前月
let future = year + "-" + (mouth + 1 + data.mouth) +"-1" //2021-6-1
let futureweek = new Date(future).getDay() //根据字符串算出当月第一天是星期几
}
btn.onclick = getDate (params)
</script>
</body>
</html>
我的思路是获取到当前月份,然后加上传入相隔的月份,getDay() 计算出当时的某月1号是星期几。然后会有一个计算
六月1号是周二。然后根据一周7天的时间差去计算 不知可行不。有问题的是第一周的周一其实是+6天就行,第一周的周三,就直接直接减法,让周数加上差值就可以
感谢了
function getdate(y, m, w, d){
let a = new Date(y + '/' + ('0' + m).slice(-2) + '/01')
let day = (w - 1) * 7 + a.getDate() - (a.getDay() - d)
a.setDate(day)
//console.log(a.toISOString())
return a
}
//获取y年m月第w周星期d的日期
let date = getdate(y, m, w, d)