Comparing two temporary variables with jquery

This is my jQuery code. In this code, #StartTime and #EndTime displays the form input tag identifier.

The time format is 00:00 AM / PM.

var starttimeval and endtimeval contain values ​​for the start and end time.

How to compare these two times, for 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 only need a time comparison function. example of getting starttimeval = 8: 00 PM and endtimeval = 9: 00 AM

+6
source share
3 answers

Ok, 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 document. (I assume you are using datetimepicker from jquery ui)

+2
source

Try the following:

  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
source

Getting values ​​from the form will return strings, it is best to convert / split 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 to deal with date and time comparisons, your first thought is to start parsing the strings and add if conditions to test various conditions like midnight ... stop, just stop.

When working with dates and times, it is best to leave the code set; you do not need to reinvent the wheel.

0
source

All Articles