奇数日期行为

时间:2011-10-03 13:00:36

标签: javascript date

使用以下代码,直到本周,该对象返回一个带有“正确”周的下拉列表,即第36周为9月5日,37为9月12日等等。

由于月份已更改为10月,因此现在错误地返回了几周。

来自LEAP.Schedule对象的代码:

    /* --- LEAP Namespace --- */
    var LEAP = {};

/* --- LEAP.Schedule Object --- */
    LEAP.Schedule = function(){//init
        this.weeks = [];
        this.calculateWeeks();
    };

    LEAP.Schedule.prototype.calculateWeeks = function(){
        this.date = new Date ( 2011, 8, 5 ); // First week of new school year
        this.num = 36; // Calendar number of this week
        this.weeks.push(new LEAP.Schedule.week(this.date, this.num));

        for (var i = 1; i < 51; i++) {
            var week = i * 7;
            var updated_date = new Date ();
            updated_date.setDate(this.date.getDate() + week);

            if (this.num > 51) {
                this.num = 0;
            }
            this.num++;

            this.weeks.push(new LEAP.Schedule.week(updated_date, this.num));
        }
    };

    LEAP.Schedule.prototype.getWeeks = function(){
        return this.weeks;
    };


/* --- LEAP.Schedule.week Object --- */
    LEAP.Schedule.week = function(n_date, n_week){
        this.week = n_week;
        this.date = n_date;

        this.year = this.date.getFullYear();
        this.month = this.date.getMonth();
        this.month += 1;
        this.day = this.date.getDate();

        var mydate = new Date(this.date);
        this.end_date = mydate.setDate(mydate.getDate() + 6);
    };

    LEAP.Schedule.week.prototype.getJSDate = function(){
        return this.date;
    };

    LEAP.Schedule.week.prototype.getStartDate = function(){
        return this.year + "-" + pad(this.month) + "-" + pad(this.day);
    };

    LEAP.Schedule.week.prototype.getEndDate = function(){
        end_of_week = new Date(this.end_date);

        var year = end_of_week.getFullYear();
        var month = pad(end_of_week.getMonth() + 1);
        var day = pad(end_of_week.getDate());

        return year + "-" + month + "-" + day;
    };

    LEAP.Schedule.week.prototype.getLabel = function(){
        return "Week " + this.week + ": " + this.day + (this.day==1||this.day==21||this.day==31?"st":this.day==2||this.day==22?"nd":this.day==3||this.day==23?"rd":"th") + " " + ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][this.month-1] + " " + this.year;
    };

pad = function (n) {
    return n>9 ? n : "0"+n;
};

代码初始化/显示此对象:

WeeklyUpdate.init = function() {
    var Scheduleobject = new LEAP.Schedule();
    var weeks = Scheduleobject.getWeeks();

    var dispHTML = '<p>weeks.length: ' + weeks.length + '</p>';

    for (var i = 0; i < weeks.length; i++) {
        if (i % 2 > 0) {
            dispHTML += '<div style="background:#ccc;">';
        } else {
            dispHTML += '<div style="background:#fff;">';
        }
        dispHTML += '<p>i: ' + i + '</p>';
        dispHTML += '<p>getJSDate: ' + weeks[i].getJSDate() + '</p>';
        dispHTML += '<p>getStartDate: ' + weeks[i].getStartDate() + '</p>';
        dispHTML += '<p>getEndDate: ' + weeks[i].getEndDate() + '</p>';
        dispHTML += '<p>getLabel: ' + weeks[i].getLabel() + '</p>';
        dispHTML += '</div>';
    }

    $('div#wrapper').html(dispHTML);

    //WeeklyUpdate.displayWeekFilter(weeks);
}

输出(三周后修剪):

weeks.length: 51

i: 0

getJSDate: Mon Sep 05 2011 00:00:00 GMT+0100 (GMT Daylight Time)

getStartDate: 2011-09-05

getEndDate: 2011-09-11

getLabel: Week 36: 5th Sep 2011

i: 1

getJSDate: Wed Oct 12 2011 13:58:02 GMT+0100 (GMT Daylight Time)

getStartDate: 2011-10-12

getEndDate: 2011-10-18

getLabel: Week 37: 12th Oct 2011

i: 2

getJSDate: Wed Oct 19 2011 13:58:02 GMT+0100 (GMT Daylight Time)

getStartDate: 2011-10-19

getEndDate: 2011-10-25

getLabel: Week 38: 19th Oct 2011

我已经看了好几次,但我感到很困惑!有任何想法吗?我相信这很明显。

干杯

2 个答案:

答案 0 :(得分:2)

初始化new Date()时,默认为当前日期。 getDate仅指日,而不是月或年。您的代码在9月份工作,因为它与开始月份相同,但由于现在是十月,您将从错误的月份中抵消。

var updated_date = new Date ();

应该是

var updated_date = new Date (2011, 8, 5);

这样你就可以从学年开始抵消,而不是今天。

演示:http://jsfiddle.net/aXgk6/

答案 1 :(得分:1)

使用此代码,并将基准日期更改为this.week变量。

我还对代码中确定新周的部分进行了更改。否则,你将在这个日历年结束时回来,询问为什么你的代码被破坏了;)

LEAP.Schedule.prototype.calculateWeeks = function(){
    this.date = new Date ( 2011, 8, 5 ); // First week of new school year
    this.num = 36; // Calendar number of this week
    this.weeks.push(new LEAP.Schedule.week(this.date, this.num));

    var not_new = true;
    for (var i = 1; i < 51; i++) {
        var week = i * 7;
        var updated_date = new Date(this.date);
        updated_date.setDate(this.date.getDate() + week);

        if (not_new && updated_date.getYear() > this.date.getYear() ) {
            not_new = this.num = 0;
        }
        this.num++;

        this.weeks.push(new LEAP.Schedule.week(updated_date, this.num));
    }
};