vue根据当前周数计算日期不使用 moment
时间: 2024-01-14 07:03:57 浏览: 31
可以使用 JavaScript 的 Date 对象进行计算,以下是一个示例代码:
```javascript
// 获取当前时间
const now = new Date();
// 获取当前年份
const year = now.getFullYear();
// 获取当前月份
const month = now.getMonth() + 1;
// 获取当前日期
const date = now.getDate();
// 获取当前星期几,注意 0 表示星期日,1 表示星期一,以此类推
const dayOfWeek = now.getDay();
// 获取当前是一年中的第几周
const weekOfYear = Math.ceil((now - new Date(year, 0, 1)) / 86400000 / 7);
// 根据当前周数计算日期
const targetDate = new Date(year, 0, 1 + (weekOfYear - 1) * 7 + dayOfWeek);
// 输出结果,注意月份需要补零
console.log(`${targetDate.getFullYear()}-${(targetDate.getMonth() + 1).toString().padStart(2, '0')}-${targetDate.getDate().toString().padStart(2, '0')}`);
```
其中,Math.ceil((now - new Date(year, 0, 1)) / 86400000 / 7) 表示计算当前是一年中的第几个周,86400000 表示一天的毫秒数。