Some useful Javascript Date function



Date.prototype.getWeek = function() { 
    var determinedate = new Date(); 
    determinedate.setFullYear(this.getFullYear(), this.getMonth(), this.getDate()); 
    var D = determinedate.getDay(); 
    if(D == 0) D = 7; 
    determinedate.setDate(determinedate.getDate() + (4 - D)); 
    var YN = determinedate.getFullYear(); 
    var ZBDoCY = Math.floor((determinedate.getTime() - new Date(YN, 0, 1, -6)) / 86400000); 
    var WN = 1 + Math.floor(ZBDoCY / 7); 
    return WN; 
}
Date.prototype.moveToFirstDayOfMonth = function(){
    return new Date( this.getFullYear(), this.getMonth());
};
Date.prototype.moveToLastDayOfMonth = function(){
    return new Date( this.getFullYear(), this.getMonth() + 1, 0);
};

Date.prototype.addDays = function(days) {
    return this.setDate(this.getDate() + days);
};

Date.prototype.firstXWeek = function(day) {
    var date = new Date();
    date = this.moveToFirstDayOfMonth();
    if(day > date.getDay()) 
        date.setDate(date.getDate() + day - date.getDay()) 
    else { 
        date.setDate(date.getDate() + (7 - date.getDay() + day ))
    }
    return date;
};
Date.prototype.lastXWeek = function(day) {
    var date = new Date();
    date = this.moveToLastDayOfMonth();
    date.setDate(date.getDate() - 7);
    if(day > date.getDay()) 
        date.setDate(date.getDate() + day - date.getDay()) 
    else { 
        date.setDate(date.getDate() + (7 - date.getDay() + day ))
    } 
    return date;
};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s