以下脚本获取所选日期及其后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>

最佳答案

这是进行计算的行:

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) {

10-02 05:08