JSFiddle - React, Tailwind, and code Playground
by 1eddy87
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
<div></div>
JavaScript
var startTime,
endTime,
currentTime = moment.utc(),
result,
time,
str = '';
function test() {
str += 'Current UTC Time <br>';
str += currentTime.format() + '<br>';
str += '------------------------------ <br>';
console.log();
startTime = moment.utc('01:00', 'HH:mm');
endTime = moment.utc('10:00', 'HH:mm');
result = currentTime.isBetween(startTime, endTime);
str += 'Test one: ' + result + '<br>';
str += 'start 01:00 - end 10:00 <br>';
str += 'result is True which is correct <br>';
str += '------------------------------ <br>';
var startPreviousDay = true;
var endNextDay = true;
startTime = moment.utc('23:00', 'HH:mm');
if (startPreviousDay) startTime = startTime.subtract(1, 'day');
console.log(startTime);
endTime = moment.utc('05:00', 'HH:mm');
if (endNextDay) endTime = endTime.add(0, 'day');
console.log(endTime);
result = currentTime.isBetween(startTime, endTime);
str += 'Test two: ' + result + '<br>';
str += 'start 23:00 - end 05:00 <br>';
//str += 'startPreviousDay = true and endNextDay = true spans across three days so the result will true<br>';
$('div').html(str);
}
test();