diff options
Diffstat (limited to 'Functions')
| -rw-r--r-- | Functions/dateFuncs.js | 26 | 
1 files changed, 24 insertions, 2 deletions
diff --git a/Functions/dateFuncs.js b/Functions/dateFuncs.js index 9b1237a..55a9372 100644 --- a/Functions/dateFuncs.js +++ b/Functions/dateFuncs.js @@ -35,8 +35,30 @@ function weekdayToNumber(s)  	}  } + +function run_at_monday_mornings(func) { +	const ms_in_h = 60/*min*/ * 60/*s*/ * 1000/*ms*/; + +	const d = new Date(); +	const hour = d.getHours(); // eg. fri 17:20 -> 17 +	const weekday = d.getDay() || 7; // 1 = monday, 7 = sunday + +	days_to_elapse = 8 - weekday; +	ms_to_elapse = ms_in_h * ( +		days_to_elapse * 24/*hours in a day*/ +		- hour // removes unneccessary hours so that we update it at 0 am +	) + 1 /*+1 so that we know that it won't be exactly midnight +	and possibly sunday. idk if this actually is needed.*/; + +	setTimeout(() => run_at_monday_mornings(func), ms_to_elapse); + +	if (weekday === 1) +		func(); +} +  module.exports = {      fromString: stringToDate,      between: isBetweenDates, -    weekdayToNumber: weekdayToNumber -}
\ No newline at end of file +    weekdayToNumber: weekdayToNumber, +    run_at_monday_mornings: run_at_monday_mornings +}  | 
