首页 > 解决方案 > 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')

你知道为什么会这样吗?

谢谢...

标签: vue.jsdatevuejs2value-of

解决方案


自己实现与日期相关的概念绝不是一个好主意。有很多你不知道的警告。我建议使用Day.js 之类的东西

特别是一年中的一周


推荐阅读