Datetimepicker 比较两个 dates/times

Datetimepicker comparing two dates/times

我浏览了几篇文章,并尝试了几个选项都无济于事。我可以比较两个包含 datetimepicker 时间的日期吗?

这是位于此处的 jquery 日期时间选择器:https://plugins.jquery.com/datetimepicker/

我有两次这样的形式:

<tr>
<td colspan="2">
<label for="">Start Time</label>
<input id="datetimepicker" class="form-control" type="text" placeholder="Click to Select"></td>
<td colspan="2">
<label for="">End-Time</label>
<input id="datetimepicker2" class="form-control" type="text" placeholder="Click to Select">
</td>
</tr>

现在我希望能够比较开始时间和结束时间,以便有总小时数字段。

我知道如何从输入等中获取值,只是不知道如何比较最后两个日期,它们的格式如下:

var startTime = $('#datetimepicker').val(); //2015/06/25 02:18

var endTime = $('#datetimepicker2').val(); //2015/06/27 12:25

var timesCompared = startTime -> endTime; //Calculate how many hours,minutes between the two dates (-> is just for visuals).

感谢您提供的任何帮助。

请试试这个

var start_actual_time  =  "01/17/2012 11:20";
var end_actual_time    =  "01/18/2012 12:25";

start_actual_time = new Date(start_actual_time);
end_actual_time = new Date(end_actual_time);

var diff = end_actual_time - start_actual_time;

var diffSeconds = diff/1000;
var HH = Math.floor(diffSeconds/3600);
var MM = Math.floor(diffSeconds%3600)/60;

var formatted = ((HH < 10)?("0" + HH):HH) + ":" + ((MM < 10)?("0" + MM):MM);
alert(formatted);

See this fiddle here

它也在处理这个问题。你可以试试。

var startDate;
         $("#startdate").datetimepicker({
                     timepicker:true,
                     closeOnDateSelect:false,
                     closeOnTimeSelect: true,
                     initTime: true,
                     format: 'd-m-Y H:m',
                     minDate: 0,
                     roundTime: 'ceil',
                     onChangeDateTime: function(dp,$input){
                               startDate = $("#startdate").val();
                                                           }
                                                           });
        $("#enddate").datetimepicker({
                     timepicker:true,
                     closeOnDateSelect:false,
                     closeOnTimeSelect: true,
                     initTime: true,
                     format: 'd-m-Y H:m',
                     onClose: function(current_time, $input){
                            var endDate = $("#enddate").val();
                            if(startDate>endDate){
                                   alert('Please select correct date');
                             }
             }
              });