英文:
Javascript for setting the date of each upcoming day of the week?
问题
以下是翻译好的部分:
So I found this little snippet of javascript that sets the date for the very next calendar Saturday
我找到了这个小段 JavaScript 代码,它设置日期为下一个日历上的星期六。
var nextSaturday = new Date();
nextSaturday.setDate(now.getDate() + (6 - 1 - now.getDay() + 7) % 7 + 1);
I can't wrap my head around this math. How would I change this for each day of the week? For instance, what would be changed to get the date for Monday?
我无法理解这个数学计算。如何将其更改以获得每周的不同日期?例如,如何更改以获取星期一的日期?
英文:
So I found this little snippet of javascript that sets the date for the very next calendar Saturday
var nextSaturday = new Date();
nextSaturday.setDate(now.getDate() + (6 - 1 - now.getDay() + 7) % 7 + 1);
I can't wrap my head around this math. How would I change this for each day of the week? For instance, what would be changed to get the date for Monday?
答案1
得分: 2
以下是代码的翻译部分:
让我们考虑今天,2023年2月24日星期五,是一周的第5天。
const today = new Date('2023-02-24');
console.log(today.getDate()) // 24
console.log(today.getDay()) // 5
现在,我们想要找到下一个星期六(明天,25日,一周的第6天)。
const saturday = new Date('2023-02-25');
console.log(saturday.getDay()) // 6
要从星期五前进到星期六,我们需要增加1天。简单地说,我们可以这样做(请注意对saturday.getDay()
使用的6
):
let nextSaturday = new Date();
nextSaturday.setDate(today.getDate() + 6 - today.getDay());
console.log(nextSaturday.getDate()) // 25
问题出在我们想要找到下一个星期一时。在这种情况下,我们可能会尝试(getDay()
返回星期一为1
):
let nextMonday = new Date();
nextMonday.setDate(today.getDate() + 1 - today.getDay());
console.log(nextMonday.getDate()) // 20
这会导致回溯,因为星期一的日期号小于星期五,所以我们从今天的日期中减去4(Monday.getDay() - today.getDay() = 1 - 5 = -4
)。为了解决这个问题,我们需要确保结果始终为正数,方法是添加7,然后取模7(请注意,在某些语言中,模运算总是返回正数,所以不需要添加7):
nextMonday = new Date();
nextMonday.setDate(today.getDate() + (1 - today.getDay() + 7) % 7);
console.log(nextMonday.getDate()) // 27
现在,这一切都很好,除了一种情况,当我们想要找到下一个星期五(3月3日)。在这种情况下,我们的问题是(5 - today.getDay() + 7) % 7 == 0
,因此我们将再次得到今天的日期。
let nextFriday = new Date();
nextFriday.setDate(today.getDate() + (5 - today.getDay() + 7) % 7);
console.log(nextFriday.getDate()) // 24
为了解决这个问题,我们需要更改代码,以便在这种情况下不是添加0,而是添加7(即我们需要一个范围7,1..6
,而不是0..6
)。做法是在取模之前从5 - today.getDay() + 7
中减去1,然后在之后再添加1:
nextFriday = new Date();
nextFriday.setDate(today.getDate() + (5 - 1 - today.getDay() + 7) % 7 + 1);
console.log(nextFriday.getDate()) // 3
通常,要使此代码适用于每周的任何给定日期,将上述代码中的5
替换为该日期的getDay()
值(0 = 星期日,1 = 星期一,...)。
完整的代码片段:
const today = new Date('2023-02-24');
console.log(today.getDate()) // 24
console.log(today.getDay()) // 5
const saturday = new Date('2023-02-25');
console.log(saturday.getDay()) // 6
let nextSaturday = new Date();
nextSaturday.setDate(today.getDate() + 6 - today.getDay());
console.log(nextSaturday.getDate()) // 25
let nextMonday = new Date();
nextMonday.setDate(today.getDate() + 1 - today.getDay());
console.log(nextMonday.getDate()) // 20
nextMonday = new Date();
nextMonday.setDate(today.getDate() + (1 - today.getDay() + 7) % 7);
console.log(nextMonday.getDate()) // 27
let nextFriday = new Date();
nextFriday.setDate(today.getDate() + (5 - 1 - today.getDay() + 7) % 7 + 1);
console.log(nextFriday.getDate()) // 3
英文:
Let's consider today, Friday, February 24th, 2023, the 5th day of the week.
const today = new Date('2023-02-24')
console.log(today.getDate()) // 24
console.log(today.getDay()) // 5
Now we want to find the next Saturday (tomorrow, the 25th, the 6th day of the week).
const saturday = new Date('2023-02-25')
console.log(saturday.getDay()) // 6
To advance from Friday to Saturday, we need to increment by 1 day. Naively, we could do this (note use of 6
for saturday.getDay()
):
let nextSaturday = new Date()
nextSaturday.setDate(today.getDate() + 6 - today.getDay())
console.log(nextSaturday.getDate()) // 25
The issue comes when we want to find the next Monday. In this case, we might try (getDay()
returns 1
for Monday):
let nextMonday = new Date()
nextMonday.setDate(today.getDate() + 1 - today.getDay())
console.log(nextMonday.getDate()) // 20
This has gone back in time because Monday's day number is less than Friday's so we end up subtracting 4 from today's date (Monday.getDay() - today.getDay() = 1 - 5 = -4
). To work around this, we need to make sure that the number is always positive, which we do by adding 7, and then taking the result modulo 7 (note in some languages modulo always returns a positive number so adding 7 is not necessary):
nextMonday = new Date()
nextMonday.setDate(today.getDate() + (1 - today.getDay() + 7) % 7)
console.log(nextMonday.getDate()) // 27
Now this all works fine except for one case, when we want to find the next Friday (the 3rd of March). In this case, our issue is that (5 - today.getDay() + 7) % 7 == 0
, so we will get today's date again.
let nextFriday = new Date()
nextFriday.setDate(today.getDate() + (5 - today.getDay() + 7) % 7)
console.log(nextFriday.getDate()) // 24
To work around this, we need to change the code so that instead of adding 0 in this case, it adds 7 (i.e. we need a range of 7,1..6
instead of 0..6
). The way to do this is to subtract 1 from 5 - today.getDay() + 7
before the modulo and then add it back afterwards:
nextFriday = new Date()
nextFriday.setDate(today.getDate() + (5 - 1 - today.getDay() + 7) % 7 + 1)
console.log(nextFriday.getDate()) // 3
In general, to adapt this code for any given day of the week, replace the 5
in the above code with the value of getDay()
(0 = Sunday, 1 = Monday, ...) for that day of the week.
Full code snippet:
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
const today = new Date('2023-02-24')
console.log(today.getDate()) // 24
console.log(today.getDay()) // 5
const saturday = new Date('2023-02-25')
console.log(saturday.getDay()) // 6
let nextSaturday = new Date()
nextSaturday.setDate(today.getDate() + 6 - today.getDay())
console.log(nextSaturday.getDate()) // 25
let nextMonday = new Date()
nextMonday.setDate(today.getDate() + 1 - today.getDay())
console.log(nextMonday.getDate()) // 20
nextMonday = new Date()
nextMonday.setDate(today.getDate() + (1 - today.getDay() + 7) % 7)
console.log(nextMonday.getDate()) // 27
let nextFriday = new Date()
nextFriday.setDate(today.getDate() + (5 - today.getDay() + 7) % 7)
console.log(nextFriday.getDate()) // 24
nextFriday = new Date()
nextFriday.setDate(today.getDate() + (5 - 1 - today.getDay() + 7) % 7 + 1)
console.log(nextFriday.getDate()) // 3
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论