P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Date
:
previousDate() & nextDate() - returns date objects of, obviously, the previous or next dates.
author:
orangechicken
[+]
,
Submitted: 10.14.02 6a
// Requires daysInMonth array and leapYear function to correctly calculate the dates Date.prototype.daysPerMonth = [31,28,31,30,31,30,31,31,30,31,30,31]; Date.prototype.leapYear = function ( year ) { if (year == null) { year = this.getFullYear() } return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0; } Date.prototype.nextDate = function(){ var d = this.getUTCDate(); var m = this.getUTCMonth(); var y = this.getUTCFullYear(); var maxDays = this.daysPerMonth[m]; if( m == 1 ) maxDays += this.leapYear(); // february d++; if( d > maxDays ) { d = 1; m++; if( m > 11 ){ m = 0; y++; } } return new Date( y, m, d ); } Date.prototype.previousDate = function(){ var d = this.getUTCDate(); var m = this.getUTCMonth(); var y = this.getUTCFullYear(); var maxDays = this.daysPerMonth[m > 0 ? m - 1 : 11 ]; if( m - 1 == 1 ) maxDays += this.leapYear(); // february d--; if( d <= 0 ) { d = maxDays; m--; if( m < 0 ){ m = 11; y--; } } return new Date( y, m, d ); }
usage
today = new Date(); yesterday = today.previousDate(); tomorrow = today.nextDate(); trace( today ); trace( yesterday ); trace( tomorrow ); /* outputs: Mon Oct 14 03:31:16 GMT-0700 2002 Sun Oct 13 00:00:00 GMT-0700 2002 Tue Oct 15 00:00:00 GMT-0700 2002 */
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@