This repository was archived by the owner on Jul 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluxon.js
More file actions
53 lines (45 loc) · 1.34 KB
/
luxon.js
File metadata and controls
53 lines (45 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { DateTime } = require('luxon')
/**
* The value returned by `DateTime.weekday` for Sunday.
* @type {Number}
*/
const SUNDAY = 7
module.exports = (firstDayOfMonth) => {
/**
* Convert the given value into an instance of `DateTime` (if necessary).
*/
if (firstDayOfMonth instanceof Date) {
firstDayOfMonth = DateTime.fromJSDate(firstDayOfMonth)
}
/**
* The number of weeks calculated by this algorithm. If the first day of the
* month is not a Sunday, then this value starts off as 1 (because even a
* partial week counts towards the total).
* @type {Number}
*/
let numberOfWeeks = (firstDayOfMonth.weekday === SUNDAY ? 0 : 1)
/**
* The date of the "current" day in the following `do` loop. This always
* starts as the first day of the specified month.
* @type {Object}
*/
let currentDay = DateTime.fromMillis(firstDayOfMonth.toMillis())
/**
* Start counting...
*/
do {
/**
* If the current day is a Sunday, add another week.
*/
if (currentDay.weekday === SUNDAY) numberOfWeeks++
/**
* Increment the current day.
*/
currentDay = currentDay.plus({ days: 1 })
/**
* Once the current date has rolled over into a different month, then the
* counting is done.
*/
} while (currentDay.hasSame(firstDayOfMonth, 'month'))
return numberOfWeeks
}