Javascript当天发货倒计时

时间:2014-12-03 00:42:37

标签: javascript javascript-events

我有一个我一直在努力的脚本,但需要一些帮助来完善它。

第一个问题是它没有考虑夏令时(CST)

第二,我在编程方面很新,我不确定我所做的改变是否完全符合逻辑。它们对我来说似乎很好但是有几只额外的眼睛看着它并没有什么坏处。

//TODO Account for CST Daylight Savings Time

function check_holiday (dt_date) {
// check simple dates (month/date - no leading zeroes)
var n_date = dt_date.getUTCDate(),
    n_month = dt_date.getUTCMonth() + 1;
var s_date1 = n_month + '/' + n_date;

if (   s_date1 == '1/1'   // New Year's Day
    //|| s_date1 == '6/14'  // Flag Day
    || s_date1 == '7/4'   // Independence Day
    || s_date1 == '11/11' // Veterans Day
    || s_date1 == '12/25' // Christmas Day
) return true;

// weekday from beginning of the month (month/num/day)
var n_wday = dt_date.getUTCDay(),
    n_wnum = Math.floor((n_date - 1) / 7) + 1;
var s_date2 = n_month + '/' + n_wnum + '/' + n_wday;

if (   s_date2 == '1/3/1'  // Birthday of Martin Luther King, third Monday in January
    || s_date2 == '2/3/1'  // Washington's Birthday, third Monday in February
    //|| s_date2 == '5/3/6'  // Armed Forces Day, third Saturday in May
    || s_date2 == '9/1/1'  // Labor Day, first Monday in September
    || s_date2 == '10/2/1' // Columbus Day, second Monday in October
    || s_date2 == '11/4/4' // Thanksgiving Day, fourth Thursday in November
) return true;

// weekday number from end of the month (month/num/day)
var dt_temp = new Date (dt_date);
dt_temp.setDate(1);
dt_temp.setMonth(dt_temp.getMonth() + 1);
dt_temp.setDate(dt_temp.getDate() - 1);
n_wnum = Math.floor((dt_temp.getDate() - n_date - 1) / 7) + 1;
var s_date3 = n_month + '/' + n_wnum + '/' + n_wday;

if (   s_date3 == '5/1/1'  // Memorial Day, last Monday in May
) return true;

// misc complex dates
//if (s_date1 == '1/20' && (((dt_date.getFullYear() - 1937) % 4) == 0) 
// Inauguration Day, January 20th every four years, starting in 1937. 
//) return true;

//if (n_month == 11 && n_date >= 2 && n_date < 9 && n_wday == 2
// Election Day, Tuesday on or after November 2. 
//) return true;
return false;
}

if (document.getElementById('countdownTimer')) {
pad = function(n, len) { // leading 0's
    var s = n.toString();
    return (new Array( (len - s.length + 1) ).join('0')) + s;
};

var timerRunning = setInterval(

    function countDown() {
        var target = 12; // 12:00hrs CST is the shipping cut-off point
        var now = new Date();
        var cstTime = now.getUTCHours() - 6;

        //Put this in a variable for convenience
        var weekday = now.getUTCDay();

        if(weekday == 0){//Sunday? Add 24hrs
            target += 24;
        }//keep this before the Sunday, trust me :>

        if(weekday == 6){//It's Saturday? Add 48hrs
            target += 48;
        }
        if(weekday != 6 || 0) {//Its not Saturday or Sunday but it is a Federal Holiday? Add 24hrs
            if(check_holiday(now)){
           target += 24;
            }
        }
        if(weekday == 5){//It's Friday and it's a Federal Holiday? Add 72hrs
            if(check_holiday(now)){
                target += 72;
            }
        }

        var hrs = (target - 1) - cstTime;
        //If between Monday and Friday, 
        //check if we're past the target hours, 
        //and if we are, abort.
        if((weekday>=1) && (weekday<=4)){
            if (cstTime > target) { //Cut off reached? Start counting from 23:59
                var hrs = 24 - (cstTime - target);
            }         

        }
        if (weekday ==5){
            if (cstTime > target) { //Cut off reached and it's Friday? Add 72hrs and start counting from 23:59
                target += 72;
                var hrs = 24 - (cstTime - target);
            }         

        }


        if (hrs < 0) hrs = 0;
        var mins = 59 - now.getUTCMinutes();
        if (mins < 0) mins = 0;
        var secs = 59 - now.getUTCSeconds();
        if (secs < 0) secs = 0;

        var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
        document.getElementById('countdownTimer').innerHTML = str;

    }, 1000
);
}

0 个答案:

没有答案