jquery datepicker + date diff计算 [英] jquery datepicker + date diff calculation

查看:118
本文介绍了jquery datepicker + date diff计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

  $(function(){
$(#arr_date)。 datepicker({dateFormat:'dd-mm-yy'});
$(#dep_date).datepicker({dateFormat:'dd-mm-yy'});
});

$(function(){
var start = $('#arr_date')。val();
var end = $('#dep_date' );
var diff = new Date(end-start);
var days = diff / 1000/60/60/24;
$('#num_nights')。val ;
});

我在stackoverflow上找到了这个日期差异解决方案,但对我来说并不适用。



根本不计算日期差异。我认为这可能是因为日期格式(dd-mm-yy)。我已经用yyyy-mm-dd格式化了,但它也没有工作。有没有人可以帮我,告诉我可能会导致什么问题?谢谢。

解决方案

您不想使用 .val() 在这里,这将给你一个字符串和相互减去的字符串是非常有用的。 datepicker有一个方法, getDate ,它给你一个Date对象,所以你可以使用它:

  var start = $('#arr_date')。 datepicker('getDate'); 
var end = $('#dep_date')。datepicker('getDate');
var days =(end-start)/ 1000/60/60/24;

演示: http://jsfiddle.net/ambiguous/TCXcX/



此外,此代码:

  $(function(){
var start = $('#arr_date')。val();
var end = #dep_date')val();
var diff = new Date(end-start);
var days = diff / 1000/60/60/24;
$('#num_nights ').val(days);
});

将在DOM准备就绪时运行,但您不希望运行直到日期选择。您需要将(更正)的计算代码绑定到按钮或观看 onSelect 事件:

  function showDays(){
var start = $('#arr_date')。datepicker('getDate');
var end = $('#dep_date')。datepicker('getDate');
if(!start ||!end)
return;
var days =(end-start)/ 1000/60/60/24;
$('#num_nights')。val(days);
}

$(#arr_date).datepicker({dateFormat:'dd-mm-yy',onSelect:showDays});
$(#dep_date).datepicker({dateFormat:'dd-mm-yy',onSelect:showDays});

另一个演示: http://jsfiddle.net/ambiguous/5BbGS/


this is my code:

$(function() {
    $( "#arr_date" ).datepicker({ dateFormat: 'dd-mm-yy' });
    $( "#dep_date" ).datepicker({ dateFormat: 'dd-mm-yy' });
});

$(function() {
    var start = $('#arr_date').val();
    var end = $('#dep_date').val();
    var diff = new Date(end - start);
    var days = diff/1000/60/60/24;
    $('#num_nights').val(days);  
});

I found this date difference solution here on stackoverflow but it doesn't work for me.

It does not calculate the date differences at all. I thought it might be because of the date formatting (dd-mm-yy). I've tried it with yyyy-mm-dd formatting too but it did not work either.

Could anyone help me and tell me what might cause the problem? Thank you.

解决方案

You don't want to use .val() here, that will give you a string and subtracting strings from each other isn't terribly useful. The datepicker has a method, getDate, that gives you a Date object so you could use that:

var start = $('#arr_date').datepicker('getDate');
var end   = $('#dep_date').datepicker('getDate');
var days   = (end - start)/1000/60/60/24;

Demo: http://jsfiddle.net/ambiguous/TCXcX/

Also, this code:

$(function() {
    var start = $('#arr_date').val();
    var end = $('#dep_date').val();
    var diff = new Date(end - start);
    var days = diff/1000/60/60/24;
    $('#num_nights').val(days);  
});

will run when the DOM is ready but you don't want it to run until the dates have been selected. You'd need to bind your (corrected) calculation code to a button or watch the onSelect events:

function showDays() {
    var start = $('#arr_date').datepicker('getDate');
    var end   = $('#dep_date').datepicker('getDate');
    if(!start || !end)
        return;
    var days = (end - start)/1000/60/60/24;
    $('#num_nights').val(days);  
}

$( "#arr_date" ).datepicker({ dateFormat: 'dd-mm-yy', onSelect: showDays });
$( "#dep_date" ).datepicker({ dateFormat: 'dd-mm-yy', onSelect: showDays });

And another demo: http://jsfiddle.net/ambiguous/5BbGS/

这篇关于jquery datepicker + date diff计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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