1 var clone = require('../lang/clone');
4 * get a new Date object representing start of period
6 function startOf(date, period){
9 // intentionally removed "break" from switch since start of
10 // month/year/etc should also reset the following periods
29 date.setMilliseconds(0);
32 throw new Error('"'+ period +'" is not a valid period');
35 // week is the only case that should reset the weekDay and maybe even
36 // overflow to previous month
37 if (period === 'week') {
38 var weekDay = date.getDay();
39 var baseDate = date.getDate();
41 if (weekDay >= baseDate) {
42 //start of the week is on previous month
45 date.setDate(date.getDate() - date.getDay());
52 module.exports = startOf;