Reputation: 2082
Background Information
I have values representing UTC start and end times that are returned to me from a database query and stored in an array like this:
[ '9145001323', '08:00', '12:00' ]
The second and third values in the array actually represents start time and end time respectively.
Question
I need to compare the current time in UTC format to see if it falls between the start time / end time range.
Code
This is the code I have right now to get the current date / time :
var currentUTC = ((new Date()).toUTCString()).split(" "); //returns an array like this: [ 'Thu,', '29', 'Sep', '2016', '15:52:32', 'GMT' ]
currentUTC[0] =currentUTC[0].substring(0, currentUTC[0].length - 1); //strip off the traling "," in the day of week name.
currentUTC[4] =currentUTC[4].substring(0, currentUTC[4].length - 3); //strip off the seconds portion of the time
What's the best way to compare the current hour / minutes I have in my currentUTC array with the data from my database query? (let's call that array the "rules" array).
I've been reading this post Compare two dates with JavaScript
but that seems to be comparing the full date whereas I just need a hour / minute comparison.
Upvotes: 1
Views: 4450
Reputation: 4489
With the use of momentjs (http://momentjs.com/docs/) it could be like this:
var y = [ '9145001323', '08:00', '12:00' ]
var t1 = moment.utc(y[1], "HH:mm");
var t2 = moment.utc(y[2], "HH:mm");
var now = moment.utc();
if( now.isAfter(t1) && now.isBefore(t2) ){
console.log(" in range ");
}
Upvotes: 0