startOf.js
1.55 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
54
var clone = require('../lang/clone');
/**
* get a new Date object representing start of period
*/
function startOf(date, period){
date = clone(date);
// intentionally removed "break" from switch since start of
// month/year/etc should also reset the following periods
switch (period) {
case 'year':
date.setMonth(0);
/* falls through */
case 'month':
date.setDate(1);
/* falls through */
case 'week':
case 'day':
date.setHours(0);
/* falls through */
case 'hour':
date.setMinutes(0);
/* falls through */
case 'minute':
date.setSeconds(0);
/* falls through */
case 'second':
date.setMilliseconds(0);
break;
default:
throw new Error('"'+ period +'" is not a valid period');
}
// week is the only case that should reset the weekDay and maybe even
// overflow to previous month
if (period === 'week') {
var weekDay = date.getDay();
var baseDate = date.getDate();
if (weekDay) {
if (weekDay >= baseDate) {
//start of the week is on previous month
date.setDate(0);
}
date.setDate(date.getDate() - date.getDay());
}
}
return date;
}
module.exports = startOf;