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

时间:2014-01-12 02:11:45

标签: javascript datetime

所以,我的代码中有一个错误。 “倒计时”功能需要返回一个文本字符串,显示开始日期和停止日期之间的天数,小时数,分钟数和秒数。如果在日历年中已经过了日期,则“changeYear”函数需要更改日期的年份值。

有些不对劲,因为文本字段没有填充日期剩余的时间:每个事件的小时:分钟:秒。只有出现的是“thisDay”字段中的当前日期。您可以在我的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

1 个答案:

答案 0 :(得分:0)

有趣的问题。该功能必须是:

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

它有效。但我也改变了很多其他的东西,请点击这里:http://jsfiddle.net/edgarinvillegas/gPjys/2/

来自玻利维亚拉巴斯的欢呼声