5

This is my jQuery code. In this code #StartTime and #EndTime is form input tag id's.

The getting time format is 00:00 AM/PM.

The var starttimeval and endtimeval contain values of getting start and end time.

How do I compare these two times, example: if(starttimeval < endtimeval){alert(message);}

  $(function() {
    $('#StartTime').datetimepicker({
        datepicker : false,
        format : 'g:i A'
    });
    $('#EndTime').datetimepicker({
        datepicker : false,
        format : 'g:i A'

    });
     var starttimeval=  $("#StartTime").val();
     var endtimeval= $("#EndTime").val();

      });

this is my form image.its show the time selection using datetimepicker plugin.

i want only time compare functionality. example getting value of starttimeval=8:00 PM and endtimeval=9:00 AM

8
  • What does starttimeval and endtimeval look like?
    – tadman
    Commented Apr 15, 2014 at 14:57
  • @tadman The getting time format is 00:00 AM/PM.
    – MaiKaY
    Commented Apr 15, 2014 at 14:57
  • I mean what do you get as actual values, not what they are theoretically. 12-hour time can't be compared directly. Having two specific examples would help get this right, as it wouldn't require installing the date picker to test it.
    – tadman
    Commented Apr 15, 2014 at 14:58
  • What about spanning midnight? Is 23:00 always more than 01:00? Commented Apr 15, 2014 at 14:59
  • when i click the submit button start time value and end time value store to variables starttimeval and endtimeval.but i need validation.so compare two times if start time less than end time give alert message.so i need time compare functionality.
    – nmkkannan
    Commented Apr 15, 2014 at 15:02

3 Answers 3

2

Well, have you tried something like this :

var dateBegin = $('StartTime').datepicker('getDate').getTime():
var dateEnd = $('EndTime').datepicker('getDate').getTime();
if (dateBegin == dateEnd)
   // some stuff

Seen in the doc. (I assume you are using datetimepicker from jquery ui)

0
0

Try this:

  var start=$("#StartTime").val();
  var starttimeval= start.split("/");
  var startdt= new Date(starttimeval[2], starttimeval[1] - 1, starttimeval[0],starttimeval[3],starttimeval[4]);

  var end=$("#StartTime").val();
  var endtimeval= end.split("/");
  var enddt= new Date(endtimeval[2], endtimeval[1] - 1, endtimeval[0],endtimeval[3],endtimeval[4]);

  if (startdt< enddt) {
    alert("startdt is before current date");
  }else{
    alert("startdtis after current date");
  }
0

Getting values from a form is going to return strings, best to convert/parse the strings into javascript Date objects and compare that

var starttime = new Date("April 14, 2014 11:00 PM");
var endtime = new Date("April 15, 2014 1:00 AM");

FIDDLE

Whenever you start dealing with date/time comparisons and your first thought is to start parsing strings and adding if conditions to test for various conditions like spanning midnight....stop, just stop.

Working with dates and times is best left to established code, no need to re-invent the wheel.

Not the answer you're looking for? Browse other questions tagged or ask your own question.