Moment detect if it's night hours

JS
S
JavaScript

Simple usage of Moment JS API to detect if current time is night or not.

1const isNightTime = () => {
2  const format = 'hh:mm:ss';
3  const startNight = moment('00:00:00', format);
4  const endNight = moment('07:00:00', format);
5  const startNightDl = moment('20:30:00', format);
6  const endNightDl = moment('23:59:59', format);
7  const currentTime = moment();
8  const night = currentTime.isBetween(startNight, endNight);
9  const nightDl = currentTime.isBetween(startNightDl, endNightDl);
10  return (night || nightDl);
11}
12
13// Do not start a new cronjob during night hours as booking is not available
14  const doNotRun = isNightTime();
15  if (!doNotRun){
16     // do stuff... it's day time
17  }

Created on 4/11/2018