16

I have two variables called 'startTime' and 'endTime'. I need to know whether current time falls between startTime and EndTime. How would I do this using JavaScript only?

var startTime = '15:10:10';
var endTime = '22:30:00';
var currentDateTime = new Date(); 
//is current Time between startTime and endTime ???

UPDATE 1:

I was able to get this using following code. You can check out the code at: https://jsfiddle.net/sun21170/d3sdxwpb/1/

var dt = new Date();//current Date that gives us current Time also

var startTime = '03:30:20';
var endTime = '23:50:10';

var s =  startTime.split(':');
var dt1 = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(),
                   parseInt(s[0]), parseInt(s[1]), parseInt(s[2]));

var e =  endTime.split(':');
var dt2 = new Date(dt.getFullYear(), dt.getMonth(),
                   dt.getDate(),parseInt(e[0]), parseInt(e[1]), parseInt(e[2]));

alert( (dt >= dt1 && dt <= dt2) ? 'Current time is between startTime and endTime' : 
                                  'Current time is NOT between startTime and endTime');
alert ('dt = ' + dt  + ',  dt1 = ' + dt1 + ', dt2 =' + dt2)
Sunil
  • 20,653
  • 28
  • 112
  • 197

5 Answers5

20
var startTime = '15:10:10';
var endTime = '22:30:00';

currentDate = new Date()   

startDate = new Date(currentDate.getTime());
startDate.setHours(startTime.split(":")[0]);
startDate.setMinutes(startTime.split(":")[1]);
startDate.setSeconds(startTime.split(":")[2]);

endDate = new Date(currentDate.getTime());
endDate.setHours(endTime.split(":")[0]);
endDate.setMinutes(endTime.split(":")[1]);
endDate.setSeconds(endTime.split(":")[2]);


valid = startDate < currentDate && endDate > currentDate
mohamed-ibrahim
  • 10,837
  • 4
  • 39
  • 51
8

You can possibly do something like this if you can rely on your strings being in the correct format:

var setDateTime = function(date, str){
    var sp = str.split(':');
    date.setHours(parseInt(sp[0],10));
    date.setMinutes(parseInt(sp[1],10));
    date.setSeconds(parseInt(sp[2],10));
    return date;
}

var current = new Date();

var c = current.getTime()
  , start = setDateTime(new Date(current), '15:10:10')
  , end = setDateTime(new Date(current), '22:30:00');

return (
    c > start.getTime() && 
    c < end.getTime());
James Hay
  • 7,115
  • 6
  • 33
  • 57
  • 1
    This code is subject to race condition: if the `start = ` line runs 1ms before midnight and `end = ` runs exactly in the midnight - you'll get false positive result. – zerkms Apr 22 '15 at 00:21
  • 7
    4 and a half years late, but updated for the race condition – James Hay Dec 10 '19 at 02:47
4

I wanted to compare a time range in the day ... so I wrote this simple logic where the time is converted into minutes and then compared.

const marketOpen = 9 * 60 + 15 // minutes
const marketClosed = 15 * 60 + 30 // minutes
var now = new Date();
var currentTime = now.getHours() * 60 + now.getMinutes(); // Minutes since Midnight

if(currentTime > marketOpen && currentTime < marketClosed){ }

Note that I have not taken UTC minutes and hours since I want to use the local time, In my case it was IST time.

Abdeali Chandanwala
  • 8,449
  • 6
  • 31
  • 45
1

A different approach: First, convert your currentDate

var totalSec = new Date().getTime() / 1000;
var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;

var numberToCompare = hours*10000+minutes*100+seconds;

cf Convert seconds to HH-MM-SS with JavaScript?

Then compare:

(numberToCompare < (endTime.split(':')[0]*10000+endTime.split(':')[1]*100+endTime.split(':')[2]*1)

or

(numberToCompare > (endTime.split(':')[0]*10000+endTime.split(':')[1]*100+endTime.split(':')[2]*1)
Community
  • 1
  • 1
Mahmal Sami
  • 689
  • 7
  • 12
1

Just another way I have for matching periods in a day, precision is in minutes, but adding seconds is trivial.

function isValid(date, h1, m1, h2, m2) {
  var h = date.getHours();
  var m = date.getMinutes();
  return (h1 < h || h1 == h && m1 <= m) && (h < h2 || h == h2 && m <= m2);
}

isValid(new Date(), 15, 10, 22, 30);
Mcm
  • 109
  • 1
  • 8
  • function isValid(date, h1, m1, h2, m2) { var h = date.getHours(); var m = date.getMinutes(); return (h1 < h || (h1 == h && m1 <= m)) && (h < h2 || (h == h2 && m <= m2)); } isValid(new Date(), 15, 10, 22, 30); Added the brackets, not to mistify the developers. – pabitranjan Jul 20 '23 at 16:38