Given that 18 May 2024 is a Saturday, how do you determine which day 1 May falls on without using new Date()?
const cur_date = 18
const cur_day = 6 // 0 - Sun, ..., 6 - Sat
const days = ['sun', 'mon', 'tue', 'wed', 'thur', 'fri', 'sat']
const res = cur_day - cur_date % 7 + 1 // Number can be reset by +7 if value is negative.
console.log(days.at(res))
When you modulo the cur_date
by 7, it will return the result to the earliest date that falls on Saturday which is 4. Afterwards, you deduct from the day itself (6 – 4) which will return 2 (Tuesday). To offset that, you will need to + 1.
Below is an example of the May 2024 Calendar.
[SUN][MON][TUE][WED][THU][FRI][SAT]
[xx] [xx] [xx] [01] [02] [03] [04]
[05] [06] [07] [08] [09] [10] [11]
[12] [13] [14] [15] [16] [17] [18]
[19] [20] [21] [22] [23] [24] [25]
[26] [27] [28] [29] [30] [31] [xx]