1

Check the Selected time should is exist in between the time slot.

var selectedTime = 01:30 AM
var startTime = 12:00 AM
var endTime   = 01:00 PM
/* need logic below in below code without date in Date Object*/
 var startTime = Date.parse('01/01/2001 '+startTime);
 var endTime = Date.parse('01/01/2001 '+endTime);

if(selectedTime <= startTime && selectedTime >= endTime)
{
  alert("Time in beween interval");
}else{
  alert("Time is not with in the time Slot");
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Snopzer
  • 1,602
  • 19
  • 31
  • 1
    Any comments about the different formats of `1:30AM` and `01:00 AM`. No space in the first one between time and AM but there is one in the later. The first one is `1` but the second one is `01`. ? – void Nov 03 '16 at 10:02
  • You will have to create a date object and parse values. – Rajesh Nov 03 '16 at 10:04
  • 2
    Use this : http://stackoverflow.com/questions/9640266/convert-hhmmss-string-to-seconds-only-in-javascript and then simply see if its bigger or smaller as its in seconds – thatOneGuy Nov 03 '16 at 10:04
  • 1
    Solution you Should Follow a Sequence so that you can check Validation, Entered value should be greater than earlier Value. – Mohammad Fareed Dec 12 '16 at 08:08
  • 1
    Don't change your existing questions to reflect a new one. If you have another question, please ask it by clicking the [Ask Question](//stackoverflow.com/questions/ask) button. – Bhargav Rao Jan 11 '17 at 12:47

1 Answers1

3

Using this : Convert HH:MM:SS string to seconds only in javascript

I have created this : https://jsfiddle.net/ceyh4ens/

Only works with 24 hour clocks for the time being, but some simple logic will get around this. The main parts are :

var selectedTimeSeconds = selectedTime.substring(0,5) + ':00'; //splits the string into hours minutes and seconds

If you want to work with 12 hour clocks, you could do more parsing here.

And now your if statements should work :)

var selectedTime = '11:30 PM'
var startTime = '12:00 AM'
var endTime = '13:00 AM'

var selectedTimeSeconds = selectedTime.substring(0,5) + ':00';
var startTimeSeconds =  startTime.substring(0,5) + ':00';
var endTimeSeconds = endTime.substring(0,5) + ':00';

var selectedTimeSecondsParsed = hmsToSecondsOnly(selectedTimeSeconds) //pass to convert to seconds function
var startTimeSecondsParsed = hmsToSecondsOnly(startTimeSeconds)
var endTimeSecondssParsed = hmsToSecondsOnly(endTimeSeconds)

console.log(selectedTimeSecondsParsed)
console.log(startTimeSecondsParsed)
console.log(endTimeSecondssParsed)

  /* need logic to convert time to Date Format */

  if (selectedTimeSecondsParsed >= startTimeSecondsParsed && selectedTimeSecondsParsed <= endTimeSecondssParsed) { //if its between
   alert("Time in beween interval");
  } else {
   alert("Time is not with in the time Slot");
  }

function hmsToSecondsOnly(str) {
  var p = str.split(':'),
    s = 0,
    m = 1;

  while (p.length > 0) {
    s += m * parseInt(p.pop(), 10);
    m *= 60;
  }

  return s;
}

EDIT

To work with 24 hours you need to check whether its AM or PM. If it's PM, add 12 hours, if it's AM do nothing. Here's the function that does that check :

function changeTime(time) {
  var thisTime;
  var thisHour = +time.substring(0, 2);
  if (time.substring(6, 8) == 'PM') {
    //add 12 hours to make it 24 hour clock
    thisHour += 12;
  }

  return thisHour + time.substring(2, 5);//concatenate with the rest
}

Then just pass your string like so :

var selectedTimeSeconds = changeTime(selectedTime);

Working updated fiddle : https://jsfiddle.net/jszuLo9o/

Community
  • 1
  • 1
thatOneGuy
  • 9,977
  • 7
  • 48
  • 90
  • 1
    As i said, the current code works with 24 hour clocks and you need to implement something to take care of AM and PM, which is simple. If last two letters are AM do nothing, if theyre PM add 12 hours to the first two numbers @MohammadFareed – thatOneGuy Nov 03 '16 at 11:00
  • Someones lazy. Updated fiddle – thatOneGuy Nov 03 '16 at 11:05