从参数添加天数

时间:2013-03-20 01:24:10

标签: javascript

我需要使用JavaScript中onclick事件处理程序的单选按钮中的参数向date对象添加一定的天数。当我简单地将日期添加到结尾时,它只会在日期结束时添加1等。有什么想法吗?

var dateObject = new Date();
var month = dateObject.getMonth();

var monthArray = new Array("January", "February", "March", "April",  
                           "May", "June", "July", "August",
                           "September","October","November","December");

function orderReadyDate(orderTime) 
{
    var dateToday = monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear();
    document.forms[0].txtPickupDate.value = dateToday;
} 

这是我的输入按钮

<input type="radio" name="item" value="print_5x7" onclick="orderReadyDate(1)" />5x7 Prints(1 day)<br />
<input type="radio" name="item" value="poster" onclick="orderReadyDate(1)" />Poster (1 day)<br />
<input type="radio" name="item" value="mug" onclick="orderReadyDate(2)" />Coffee Mug (2 days)<br />
<input type="radio" name="item" value="shirt" onclick="orderReadyDate(3)" />T-shirt (3 days)</p>

2 个答案:

答案 0 :(得分:0)

var dateToday = monthArray[month] + " " 
        + (dateObject.getDate() + orderTime) 
        + ", " + dateObject.getFullYear();

答案 1 :(得分:0)

您需要尝试: - 注意setDate方法,它将添加日期。

 var newdate = new Date();
 newdate.setDate(newdate.getDate() + orderTime);
 var dateToday = monthArray[newdate.getMonth()] + " " + 
                 newdate.getDate() + ", " + 
                 newdate.getFullYear();