javascript - Comparing two time variables with jquery - Stack Overflow

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

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 pare 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();

      });

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

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 pare 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();

      });

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

Share edited Apr 15, 2014 at 15:57 nmkkannan asked Apr 15, 2014 at 14:52 nmkkannannmkkannan 1,3035 gold badges27 silver badges49 bronze badges 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 pared 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? – PaulProgrammer 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 pare two times if start time less than end time give alert message.so i need time pare functionality. – nmkkannan Commented Apr 15, 2014 at 15:02
 |  Show 3 more ments

3 Answers 3

Reset to default 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)

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");
  }

Getting values from a form is going to return strings, best to convert/parse the strings into javascript Date objects and pare 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 parisons 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742263764a4411394.html

相关推荐

  • javascript - Comparing two time variables with jquery - Stack Overflow

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

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信