vue.js - undefined (reading 'valueOf') 在 Vue 中查找周数
问题描述
我试图找到一周的数字。所以为此我的功能是:
getWeekNumber(date) {
const temp_date = new Date(date.valueOf());
const dayn = (date.getDay() + 6) % 7;
temp_date.setDate(temp_date.getDate() - dayn + 3);
const firstThursday = temp_date.valueOf();
temp_date.setMonth(0, 1);
if (temp_date.getDay() !== 4) {
temp_date.setMonth(0, 1 + ((4 - temp_date.getDay() + 7) % 7));
}
return 1 + Math.ceil((firstThursday - temp_date) / 604800000);
},
我在我的其他功能中使用此功能来查找产品:
findProducts(products, date) {
return products.filter((product) => {
const formated_date = this.setDateFormat(product.attributes.date);
console.log(formated_date);
console.log(formated_date.valueOf());
return (
formated_date.getDate() === date.getDate() &&
formated_date.getMonth() === date.getMonth() &&
formated_date.getFullYear() === date.getFullYear() &&
this.getWeekNumber(formated_date) === this.currentWeekNumber
);
});
},
但我的问题出在控制台的 findProducts 函数内部,我可以看到如下输出:Tue Sep 28 2021 02:00:00 GMT+0200 (Central European Summer Time) Table.vue:144 1632787200000
但是每当它转到 getWeekNumber 函数时,我都会收到此错误: vue.esm.js:1897 TypeError: Cannot read properties of undefined (reading 'valueOf')
你知道为什么会这样吗?
谢谢...
解决方案
自己实现与日期相关的概念绝不是一个好主意。有很多你不知道的警告。我建议使用Day.js 之类的东西
特别是一年中的一周
推荐阅读
- c++ - 用于 Motif 库(C)的水平绘制的 RowColumn 类?
- ios - 如何仅在选择时显示 Tabbar 项的标题 rest tabbar 项在 Swift 中将没有标题
- android - 我无法在 android 10 手机上读取文件,但它可以在 android 7 上运行
- azure - 如何在 azure AD 上将应用程序权限添加到我自己的应用程序
- python - 如何在 Selenium 中设置执行测试的顺序
- python - 迭代数据帧的第 n 行
- tensorflow - 在 Tensorflows 现有 Estimator 中使用文本作为特征列
- sandbox - Coinbase Pro Sandbox:如何存入测试资金?
- sql-server - SQL 用于通过连字符拆分字符串,然后为每个长度为 3 个或更多字符的块生成所有后缀?
- php - 从mysql数据库中获取多个图像