您好,我的日期格式为03/08/12
,其中03表示月份,08表示日期,12表示年份。现在我想在其中添加几天。我试过以下但没有得到确切的结果
checkindate = new Date($("#checkindate").val());
checkindate.setDate(checkindate.getDate()+no_of_nights);
$("#checkoutdate").val((checkindate.getMonth()+1)+"/"+checkindate.getDate()+"/"+checkindate.getYear());
但是会给出意想不到的结果,我无法理解它添加的日期部分。任何人都可以告诉我该怎么做?
此致
答案 0 :(得分:0)
你现在拥有的东西在所有情况下都不会起作用,因为你将no_of_nights
添加到Day值,这意味着你可能有31 + 5,setDate()不会给你预期的结果,值为36。
您必须弄清楚要添加的月数,天数,年数等。
Javascript可以在1970年1月1日的几毫秒内完成,而不是正常的时间。
答案 1 :(得分:0)
您的代码完美无缺,但确保no_of_nights
不是字符串。这可能是您的问题:如果checkindate.getDate()+no_of_nights
是字符串,则添加操作no_of_nights
将成为连接操作。
8 + 5 = 13
8 + "5" = "85"
非常不同!
解决方案:使用parseInt(no_of_nights)
。