查找两个日期之间的天Jquery/JS [英] Find days between two dates Jquery/JS

查看:39
本文介绍了查找两个日期之间的天Jquery/JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上进行了广泛的搜索,到目前为止,我发现的所有代码都无法正常工作,而我编写的代码也都可以这样做,所以我开始担心这是不可能的.

I have searched far and wide for this problem and so far none of the codes I have found failed to work and ones I have made also do the same, So I am starting to worry it isnt possible.

我们有两个日期,格式为mm/dd/yy,而不是dd/mm/yy,例如11/30/2012.

We have two dates in mm/dd/yy format not dd/mm/yy so 11/30/2012 for example.

然后我使用以下脚本:

if ( jQuery("#book_room_type").val() == "La Maison" ) {
    Date.prototype.DaysBetween = function() {  
        var intMilDay = 24 * 60 * 60 * 1000;  
        var intMilDif = arguments[0] - this;  
        var intDays = Math.floor(intMilDif/intMilDay);  
        return intDays;  
    }  
    var d1 = new Date(jQuery("#dateto").val());  
    var d2 = new Date(jQuery("#datefrom").val());  
    alert("you only have".d1.DaysBetween(d2)); 

    return false;   
}   

但这并没有返回dateto和datefrom字段之间的天数,它现在使我发疯,请提出任何建议.

This however does not return the ammount of days between the dateto and datefrom fields, it is now driving me crazy, please any suggestions are appreciated.

谢谢西蒙

参考开尔文的建议:

if ( jQuery("#book_room_type").val() == "La Maison" ) {
    alert("test");
    var dategap = (jQuery("#dateto").val().getTime() - jQuery("#datefrom").getTime()) / (1000 * 60 * 60 * 24);
    alert(dategap); 
    alert("test2");
    return false;   
}   

这在第二次警报时失败,并且没有警报日期.

This failed on the second alert and did not alert dates.

推荐答案

代码有效,但是在警报中使用点而不是+会出现串联错误

Code works, but you have a concatenation error using dot instead of + in the alert

演示: http://jsfiddle.net/YgY5c/

这篇关于查找两个日期之间的天Jquery/JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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