Datetimepicker比较两个日期/时间

时间:2015-06-02 06:32:35

标签: jquery html datetimepicker

我查看了几个帖子,并尝试了几个选项无济于事我可以比较两个日期,包括datetimepicker的时间。

这是位于此处的jquery datetimepicker: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).

感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:2)

请试试这个

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

答案 1 :(得分:1)

它也在使用它。你可以尝试一下。

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