1
0

Another day, another js date edge-case

This commit is contained in:
kolaente
2021-09-01 22:12:46 +02:00
parent c42f8ca9f8
commit 8b4efeeb11
2 changed files with 11 additions and 2 deletions

View File

@ -291,7 +291,16 @@ const getDayFromText = (text: string) => {
}
const date = new Date()
date.setDate(parseInt(results[0]))
const day = parseInt(results[0])
date.setDate(day)
// If the parsed day is the 31st but the next month only has 30 days, setting the day to 31 will "overflow" the
// date to the next month, but the first.
// This would look like a very weired bug. Now, to prevent that, we check if the day is the same as parsed after
// setting it for the first time and set it again if it isn't - that would mean the month overflowed.
if(day === 31 && date.getDate() !== day) {
date.setDate(day)
}
if (date < new Date()) {
date.setMonth(date.getMonth() + 1)