在JS中,为什么在我映射数组时,每个月第一个月的日期会被整理到上个月?

发布于 2025-02-13 03:18:46 字数 1084 浏览 0 评论 0原文

我如何获得每个月第一个出现的费用以及该月其余费用的出现? 在这种情况下,我有一个组件,它可以将数据库中所有费用的状态数据带入状态数据,然后我调用一个函数以将其映射以将每个费用放在其相应月份中:

const expensesData = useSelector((state)=>state?.financialsReducer?.expenses)

const monthlyExpenses = (m) => {
        const expenseArray = expensesData?.map((e)=>{
            if(new Date(e.date).getMonth() === m) {
                return e.amount
            } else {
                return null
            }
        })
        return expenseArray.reduce((accumulator, current)=> accumulator + current, 0).toFixed(2)
    }

const juneExpenses = monthlyExpenses(5)
const julyExpenses = monthlyExpenses(6)

问题是,问题是,发生的费用是每个月的第一个出现在上个月的结果中。例如,如果我在2022年7月1日有费用,则会出现在Junexpenses的结果中。

这是一个具体的例子。仅当调用M = 5的每月Expensens函数时(即6月份)才会出现此对象。费用的日期显然是7月/7月的。

{_id: '1212saf23423df04fb10', date: '2022-07-01T00:00:00.000Z', year: '2022', category: 'rent', amount: 1176, …}
amount: 1176
category: "rent"
date: "2022-07-01T00:00:00.000Z"
details: ""
year: "2022"
__v: 0
_id: "1212saf23423df04fb10"
[[Prototype]]: Object

How do I get expenses that occur on the first of each month to appear with the rest of the expenses for that month?
In this case, I have a component that brings in the state data for all of the expenses in the database and then I call a function to map over it to place each expense in its corresponding month:

const expensesData = useSelector((state)=>state?.financialsReducer?.expenses)

const monthlyExpenses = (m) => {
        const expenseArray = expensesData?.map((e)=>{
            if(new Date(e.date).getMonth() === m) {
                return e.amount
            } else {
                return null
            }
        })
        return expenseArray.reduce((accumulator, current)=> accumulator + current, 0).toFixed(2)
    }

const juneExpenses = monthlyExpenses(5)
const julyExpenses = monthlyExpenses(6)

The problem is, expenses that occur on the first of each month are appearing in the results for the previous month. For example, if I have an expense on July 1 2022, it appears in the results for juneExpenses.

Here's a specific example. This object appears only when the monthlyExpenses function is called with m = 5 (that is, for the month of June). The expenses' date is clearly for the month of July/7.

{_id: '1212saf23423df04fb10', date: '2022-07-01T00:00:00.000Z', year: '2022', category: 'rent', amount: 1176, …}
amount: 1176
category: "rent"
date: "2022-07-01T00:00:00.000Z"
details: ""
year: "2022"
__v: 0
_id: "1212saf23423df04fb10"
[[Prototype]]: Object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2025-02-20 03:18:47

如果仅在一个月的第一个月发生,那可能是时区问题。也就是说,您的数据来自UTC,但是您的测试环境是UTC-(某物),因此您的日期实际上是在上个月的最后一天。

您可以通过标准化日期设置的位置来解决此问题。

编辑:通过“标准化在哪里”,我的意思是“标准化您的日期”。因此,基本上,在数据集中添加一个时区,或在测试环境中添加时区偏移。

If it's happening only the first of the month, it's likely a timezone issue. That is, your data is from UTC, but your testing environment is UTC-(something), so your date is effectively on the last day of the previous month.

You can fix this by standardizing where your dates are set.

Edit: by "standardizing where," I mean "standardizing in what timezone" your dates are set. So basically, add a timezone to your datasets, or add a timezone offset to your testing environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文