jQuery DateTimePicker UI获取两个datetimepickers之间的总天数 [英] JQuery DateTimePicker UI getting total days between two datetimepickers

查看:89
本文介绍了jQuery DateTimePicker UI获取两个datetimepickers之间的总天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我拥有的两个自定义DateTimePickers中获取总天数.

i am trying to get the total days from the two customized DateTimePickers i've got.

我已经尝试过此功能:

 function dateDifference() {
if($("#fradato").val()!='' && $("#fradato").val()!='') {
var diff = ($("#fradato").datepicker("getDate") - $("#tildato").datepicker("getDate")) / 1000 / 60 / 60 / 24;
$("#antalldager").val(diff)

我遇到的问题是,无论我在哪里尝试调用此函数,DateTimePicker都将停止工作.那么我应该在哪里/如何调用此函数,或者有更好的方法呢?

The problem i've got is that wherever i try to call this function, the DateTimePicker stop working. So where/how should i call this function or is there a better way to do it?

我想在此文本框内显示工资日:

I want to display the toal days inside of this text-box:

 <div class="form-group">
   <label class="control-label col-sm-1 col-sm-offset-3"     for="antalldager">Antall dager:</label>
 <div class="col-sm-2">
<input type="text" class="form-control" id="antalldager" disabled>

这是我的html:

 <script>valgavdato() </script>
<div  id="visdager" style="display: none;">
<label class="control-label col-sm-2 col-sm-offset-3" for="fradato">Book fra:</label>
  <div class="container">
      <div class="row">
          <div class='col-sm-2'>
              <div class="form-group">
                  <div class='input-group date'>
                      <input type="text" id="fradato" class="form-control"/>
                      <span class="input-group-addon">
                          <span class="glyphicon glyphicon-calendar"  ></span>
                      </span>
                  </div>
              </div>
          </div>
      </div>
  </div>
  <div id="vistildato" style="display: none;">
    <label class="control-label col-sm-2 col-sm-offset-3" for="tildato">Book til:</label>
  <div class="container">
      <div class="row">
          <div class='col-sm-2'>
              <div class="form-group">
                  <div class='input-group date'>
                      <input type="text" id="tildato" class="form-control" />
                      <span class="input-group-addon">
                          <span class="glyphicon glyphicon-calendar"></span>
                      </span>
                  </div>
              </div>
          </div>
      </div>
  </div>

JS:

   $( function valgavdato() {
var dateFormat = "mm/dd/yy",
    to_MaxDate = 13;    // From date + this = to maxDate

    from = $( "#fradato" )
.datepicker({
    minDate: '0+',
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 1

})
.on( "change", function() {
    var PickedDate = getDate( this );
    // See that date is in UTC format.
    console.log( "From DatePicker: "+JSON.stringify(PickedDate) );

    // Process the picked date.
    var tempDay = PickedDate.getDate() + to_MaxDate; // Add a number of days to the picked date.
    var tempMonth = PickedDate.getMonth() + 1;   // Because months are zero based.
    var tempYear = PickedDate.getYear() + 1900; // Because years are 1900 based
    console.log( "Temp date: "+ tempYear+"/"+tempMonth+"/"+tempDay +" --- It may look impossible... But Date() handles it.");

    // Create a date object in a UTC format.
  var newMaxDate = new Date(Date.UTC(tempYear, tempMonth-1, tempDay, 0, 0, 0));
    console.log(  "New max date: : "+ JSON.stringify(newMaxDate) );

    // Set the minDate ans maxDate options.
    to.datepicker( "option", {"minDate": PickedDate, "maxDate": newMaxDate});
}),
    to = $( "#tildato" ).datepicker({
        maxDate: '14+',
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1
    })
.on( "change", function() {
    from.datepicker( "option", "maxDate", getDate( this ) );
});

function getDate( element ) {
    var date;
    try {
        date = $.datepicker.parseDate( dateFormat, element.value );
    } catch( error ) {
        date = null;
    }
    return date;
}
} );

function dateDifference() {
if($("#fradato").val()!='' && $("#fradato").val()!='') {
    var diff = ($("#fradato").datepicker("getDate") - $("#tildato").datepicker("getDate")) / 1000 / 60 / 60 / 24;
    $("#antalldager").val(diff)
}
}

推荐答案

我认为@JanisP建议很好.您还可以制作一个轻量级的函数,例如:

I think @JanisP suggest is good. You can also make a lightweight function like:

function dateDifference(start, end) {
  // Assume Start and End string format: dd/mm/yyyy
  var d1 = new Date(start);
  var d2 = new Date(end);
  // d2 - d1 gives result in milliseconds
  // calculate number of days by Math.abs((d2-d1)/86400000, as 24*3600*1000 = 86400000
  return Math.abs((d2-d1)/86400000)
}

在此处找到此内容: jQuery UI Datepicker-日期之间的天数

更新

jQuery UI Datepicker选择一个日期范围示例,我创建了这个工作示例: https://jsfiddle.net/Twisty/r8wx6afk/

From the jQuery UI Datepicker Select a Date Range example, I created this working example: https://jsfiddle.net/Twisty/r8wx6afk/

仅当两个字段在change事件期间都有日期时,才计算天数.

It calculates the number of days only when both fields have a date during the change event.

这篇关于jQuery DateTimePicker UI获取两个datetimepickers之间的总天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆