JQuery计算2个日期文本框中的日差

时间:2010-04-09 17:24:09

标签: jquery datetime

HI Guys,

我有2个带日历扩展器的asp.net texbox。我想找出其中一个日期控件发生变化时两个日期之间的天数。如何使用jquery或javascript实现这一目标?

9 个答案:

答案 0 :(得分:82)

这应该可以解决问题

var start = $('#start_date').val();
var end = $('#end_date').val();

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

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

实施例

var start = new Date("2010-04-01"),
    end   = new Date(),
    diff  = new Date(end - start),
    days  = diff/1000/60/60/24;

days; //=> 8.525845775462964

答案 1 :(得分:5)

1)Html

<input type="text" id="firstDate" name="firstDate"/>
<input type="text" id="secondDate" name="secondDate"/>

2)Jquery

$("#firstDate").datepicker({

}); 
$("#secondDate").datepicker({
    onSelect: function () {
        myfunc();
    }
}); 

function myfunc(){
    var start= $("#firstDate").datepicker("getDate");
    var end= $("#secondDate").datepicker("getDate");
    days = (end- start) / (1000 * 60 * 60 * 24);
    alert(Math.round(days));
}

Jsfiddle工作示例here

答案 2 :(得分:1)

嗨, 这是我计算两个日期之间的差异的示例

    <!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <title>JS Bin</title>
</head>
<body>
  <br>
<input class='fromdate' type="date"  />
<input class='todate' type="date" />
<div class='calculated' /><br>
<div class='minim' />  
<input class='todate' type="submit" onclick='showDays()' />

</body>
</html>

这是计算差异的函数:

function showDays(){

     var start = $('.fromdate').val();
     var end = $('.todate').val();

     var startDay = new Date(start);
     var endDay = new Date(end);
     var millisecondsPerDay = 1000 * 60 * 60 * 24;

     var millisBetween = endDay.getTime() - startDay.getTime();
     var days = millisBetween / millisecondsPerDay;

      // Round down.
       alert( Math.floor(days));

}

希望我能对您有所帮助

答案 3 :(得分:0)

两个日期之间的天数计算。

    $(document).ready(function () {
        $('.submit').on('click', function () {
            var startDate = $('.start-date').val();
            var endDate = $('.end-date').val();

            var start = new Date(startDate);
            var end = new Date(endDate);

            var diffDate = (end - start) / (1000 * 60 * 60 * 24);
            var days = Math.round(diffDate);
        });
    });

答案 4 :(得分:0)

这是我使用Math.floor()函数完成的方式:

var start = new Date($('#start').val());
var end = new Date($('#end').val());
var diff = Math.floor((end-start) / (365.25 * 24 * 60 * 60 * 1000));
console.log(diff);

您还可以使用Math.round()函数以这种方式进行操作:

var start = new Date($('#start').val());
var end = new Date($('#end').val());
var diff = new Date(end - start) / (1000 * 60 * 60 * 24 * 365.25);
var age = Math.round(diff);
console.log(age);

答案 5 :(得分:0)

   **This is a simple way of getting the DAYS between two dates** 
    var d1 = moment($("#StartDate").data("DateTimePicker").date());
    var d2 = moment($("#EndDate").data("DateTimePicker").date());

    var diffInDays = d2.diff(d1, 'days');

    if (diffInDays > 0)
    {
        $("#Total").val(diffInDays);
    }
    else
    {
        $("#Total").val(0);
    }

答案 6 :(得分:0)

   @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> map = new HashMap<>();
            map.put("email", txtEmail.getText().toString().trim());
            map.put("name", name_from_input);
            map.put("password", txtPassword.getText().toString());
            return map;
        }

答案 7 :(得分:0)

dplyr

答案 8 :(得分:-2)

这是在两个日期之间获得DAYS的简单方法

var nites = dateDiff(arriveDate,departDate,"D");