简体   繁体   English

JavaScript / jQuery-计算天数,接收输入数据和输出数据

[英]JavaScript / jQuery - calculate number of days, receiving input data and output data

I have two fields of data entry, date of entry and date of out, which receive data using a datepicker. 我有两个数据输入字段,输入日期和输出日期,它们使用日期选择器接收数据。 Is it possible to calculate the number of days, take into account the values of the date of entry and date of out, and save it in the field number of nights? 是否可以计算天数,将输入日期和离开日期的值考虑在内,并将其保存在字段的夜晚数中? How can I do this? 我怎样才能做到这一点?

HTML 的HTML

    <div class="form-group">
        <label class="col-md-4 control-label">Date Entry</label>
        <div class="col-md-4 inputGroupContainer">
            <div class="input-group">
                <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
                <input name="DateEntry" id="DateEntry"  class="form-control" type="text">
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-4 control-label">DateOut</label>
        <div class="col-md-4 inputGroupContainer">
            <div class="input-group">
                <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
                <input name="DateOut" id="DateOut" class="form-control" type="text">
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-4 control-label">NºNights</label>
        <div class="col-md-4 inputGroupContainer">
            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-list-ol"></i></span>
                <input name="Nights" class="form-control" type="text">
            </div>
        </div>
    </div>
var start = $('#DateEntry').val();
var end = $('#DateOut').val();

// end - start returns difference in milliseconds 
var diff = new Date(end - start);

// get nights
var days = diff/1000/60/60/24;

//set nights
$('#Nights').val(days)

This is client side code. 这是客户端代码。 You have to call this from document.load or may be onchange event of both entry and out dates. 您必须从document.load调用它,或者可能是输入和输出日期的onchange事件。

Here's a similar approach with the hook to handle the text changing. 这是使用钩子处理文本更改的类似方法。

<html>
<head>
    <title>Date Diff in Nights</title> 
</head>
<body>
    <div id="container">
        <span class="input-group-addon"><i class="glyphicon glyphicon-time">entry</i></span>
        <input name="DateEntry" id="DateEntry" class="form-control" type="text" >

        <span class="input-group-addon"><i class="glyphicon glyphicon-time">out</i></span>
        <input name="DateOut" id="DateOut" class="form-control" type="text" >

        <span class="input-group-addon"><i class="fa fa-list-ol">nights</i></span>
        <input name="Nights" id="Nights" class="form-control" type="text">
    </div>
    <script type="text/javascript">
        var input = document.getElementById('DateEntry');
        input.addEventListener('input', calcNights);
        var input2 = document.getElementById('DateOut');
        input2.addEventListener('input', calcNights);
        function calcNights() {

            try {
                var nightsout = document.getElementById('Nights');
                var element = document.getElementById('DateEntry');
                var entryDate = new Date(element.value);
                var out = document.getElementById('DateOut');
                var outDate = new Date(out.value);
                // Set to noon to avoid any DST errors
                outDate.setHours(12, 0, 0);
                entryDate.setHours(12, 0, 0);
                var difference = outDate - entryDate;
                var nights = Math.round(difference / 8.64e7);
                if (isNaN(nights) || nights < 0) {
                    nightsout.value = '';
                    return;
                }
                nightsout.value = nights;
            }
            catch (ex) {
                //ignore
            }
        } 
    </script> 
</body>
</html>

Here is working code with your update of date format being DD/MM/YYYY : 这是工作代码,您更新的日期格式为DD/MM/YYYY

jQuery jQuery的

I highly suggest using Momentjs. 我强烈建议使用Momentjs。 It is user friendly and very powerful when working with dates & times! 它是用户友好的,并且在处理日期和时间时非常强大! That is what my solution will use. 这就是我的解决方案将要使用的。 Otherwise you will have to do a bunch of parsing in order to match your date format. 否则,您将不得不进行大量解析以匹配您的日期格式。 Just make sure that your format doesn't change or else you will have to edit this! 只需确保您的格式没有改变,否则您将不得不对其进行编辑!

$(document).ready(function() {

  $('#TestOne').datetimepicker({
    format: 'DD/MM/YYYY HH:mm:ss',
  });

  $('#TestTwo').datetimepicker({
    format: 'DD/MM/YYYY HH:mm:ss',
  });

   $("#DateOut").blur(function() {
     var str1 = $("#DateEntry").val();
     var str2 = $("#DateOut").val();

     var dateEntry = moment(str1, "DD/MM/YYYY");
     var dateOut = moment(str2, "DD/MM/YYYY");
     var range = dateOut.diff(dateEntry, 'days');

   $("#NightsBetween").val(range);
  });
});

UPDATED FIDDLE 更新场

Here is a working JSFiddle 这是一个正在工作的JSFiddle

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM