调整日期差异

时间:2014-03-31 04:13:00

标签: javascript date calendar

以下脚本正在获取所选日期加上59天后,我需要帮助将其修改为前后30天(选定日期前30天+所选日期后选定日期+ 30天)。

<script lang="javascript">
    function setOptions(selected_holiday) 
    { 
    sh = new Date();
    aDay = 24*60*60*1000;
    sh_year = selected_holiday.substr(0, 4) ;
    sh_month = selected_holiday.substr(4, 2) - 0;
    sh_day = selected_holiday.substr(6, 2) ;
    sh.setFullYear(sh_year,sh_month-1,sh_day);
    sh.setTime(sh.getTime()+aDay);

    sh_max = new Date(sh.getTime()+(59*aDay)+aDay);
    var defoff_date = document.deferform.defoff_date; 
    defoff_date.options.length = 0; 

    for (i=sh.getTime();i<sh_max;i+=aDay) {
    date = new Date(i);
    year = date.getYear();
    slash = "/";
    if (year < 1900) year += 1900;
    strDay = "0"+ date.getDate();
    strDay =strDay.substr(strDay.length-2,2);
    strMonth = "0"+ (date.getMonth()+1);
    strMonth = strMonth.substr(strMonth.length-2,2);
    strDate = "" + strMonth + slash + strDay + slash + year;
    defoff_date.options[defoff_date.options.length] = new Option(strDate); 
    }
</script>

1 个答案:

答案 0 :(得分:2)

这是进行计算的行:

sh_max = new Date(sh.getTime()+(59*aDay)+aDay);

在此之后,shsh_max是“现在”和“现在六十天后”。

如果您将其更改为

sh_min = new Date(sh.getTime()-30*aDay);
sh_max = new Date(sh.getTime()+30*aDay+aDay);

然后sh_minsh_max将是“前三十天”和“三十一天后”。您还需要更改循环以反映新变量名称:

for (i=sh_min.getTime();i<sh_max;i+=aDay) {