如何使用 javascript 和 Date 对象在事件发生前的剩余时间填充输入字段



所以,我的代码中某处有一个错误。"countdown"函数需要返回一个文本字符串,显示开始日期和停止日期之间的天数、小时数、分钟数和秒数。如果日期已在日历年中过去,则"changeYear"函数需要更改日期的年份值。

有些不对劲,因为文本字段没有填充每个事件的剩余时间(以 days:hrs:min:sec 为单位)。唯一显示的是"这一天"字段中的当前日期。您可以在我的 http://jsfiddle.net/gPjys/jsfiddle 中看到这一点,其中"事件倒计时"字段为空。

另请参阅以下代码中的"此处可能是问题所在":

    function showCountdown(){
        var today = new Date();
        var Date1 = "Jan 14, 2011 at 10:00 a.m.";
        var Date2 = "May 21, 2011 at 12:00 p.m.";
        var Date3 = "Jul 4, 2011 at 9:00 p.m.";
        var Date4 = "Sep 1, 2011 at 12:00 p.m.";
        var Date5 = "Dec 1, 2011 at 11:30 a.m.";
        var Date6 = "Dec 31, 2011 at 3:30 p.m.";   
        document.eventform.thisDay.value = showDateTime(today);
        changeYear(today, Date1);
        changeYear(today, Date2);
        changeYear(today, Date3);
        changeYear(today, Date4);
        changeYear(today, Date5);
        changeYear(today, Date6);
        document.eventform.count1.value = countdown(today, Date1);
        document.eventform.count2.value = countdown(today, Date2); 
        document.eventform.count3.value = countdown(today, Date3); 
        document.eventform.count4.value = countdown(today, Date4); 
        document.eventform.count5.value = countdown(today, Date5);
        document.eventform.count6.value = countdown(today, Date6);
}

    function showDateTime(time) {
        date = time.getDate();
        month = time.getMonth()+1;
        year = time.getFullYear();
        second = time.getSeconds();
        minute = time.getMinutes();
        hour = time.getHours();
        ampm = (hour < 12) ? " a.m." : " p.m.";
        hour = (hour > 12) ? hour - 12 : hour;
        hour = (hour === 0) ? 12 : hour;
        minute = minute < 10 ? "0"+minute : minute;
        second = second < 10 ? "0"+second : second;
        return month+"/"+date +"/"+year+" at "+hour+":"+minute+":"+second+ampm;
    }
    //HERE MAY BE WHERE THE PROBLEM LIES
    function changeYear(today, holiday){
    /*
        insert a function named 'changeYear' that will
        change a date's year value if the date has already been 
        passed in the current calendar year
    */
        var year = today.getFullYear();
    //The holiday parameter is used to store the date object representing one of the
    //events in the event column of the form
        holiday.setFullYear(year);
        holiday = (holiday < today) ? year += 1 : year;
        holiday.setFullYear(year);
    }
    function countdown(start, stop){
    /*
      will return a text string displaying the number of days,
      hours, minutes, and seconds between a starting date and a
      stopping date. 
    */  
        var time = stop - start;
    /*
      convert the time difference into days, hours, minutes, seconds
      and return the following text string
      days days, hours hrs, minutes mins, seconds secs   
    */
        var days = time.getDate();
        var hours = (days - Math.floor(days))*24;
        var minutes = (hours - Math.floor(hours))*60;
        var seconds = (minutes - Math.floor(minutes))*60;
        return days + "days" + hours + "hrs" + minutes + "mins" + seconds + "secs";
    }

http://jsfiddle.net/gPjys/

提前感谢!干杯!-QS

有趣的问题。该函数必须是:

function changeYear(today, holiday){
    var todayYear = today.getFullYear();
    holiday.setFullYear(todayYear);
    if(today > holiday){        
        holiday.setFullYear(todayYear + 1);
    }    
}

它有效。但我也改变了许多其他东西,检查这里:http://jsfiddle.net/edgarinvillegas/gPjys/2/

干杯,来自玻利维亚拉巴斯

最新更新