I wrote these functions for my calendar/datepicker class.
Date.implement({
minute: 60,
hour: 3600,
day: 86400,
shortDayLen: 3,
shortMonthLen: 3,
isLeapYear: function(){ // Are we in a leap year ?
if(this.getFullYear() % 4 == 0 && !(this.getFullYear() % 400 == 0) ? 1 : 0)
return true;
else return false;
},
days: [
'Sunday', 'Monday', 'Tuesday','Wednesday',
'Thursday', 'Friday', 'Saturday'
],
months: [
'january','fabruary','march', 'april',
'may','june','july','august',
'september','october','november','december'
],
monthLength: function(){ // How many days in a given month ?
var numDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if(this.isLeapYear()) numDay[1] = 29;
return numDay[this.getMonth()];
},
getMonthName: function(){ // The name of the month
return this.months[this.getMonth()];
},
getDayName: function(){ // The name of the day
return this.days[this.getDay()];
},
format: function(how) {
// Format a date in human language : Mercredi 20 janvier 2009
// or american language : 01-20-2009 :) by replacing %CHAR
//by the date's value
// Shorter day name : mon
how = how.replace(/%a/g, this.getDayName().substr(0, this.shortDayLen));
// Day name : monday
how = how.replace(/%A/g, this.getDayName());
// Number of the day in the week (1-7) : 1 = monday
how = how.replace(/%u/g, (this.getDay() + 6) % 7 + 1);
// Number of the day in the week (0-6) : 0 = sunday
how = how.replace(/%w/g, this.getDay());
// Shorter month name : mar
how = how.replace(/%b/g, this.getMonthName().substr(0, this.shortMonthLen));
// Month name : march
how = how.replace(/%B/g, this.getMonthName());
// Number of the month (1-12) : 3
how = how.replace(/%m/g, this.getMonth() + 1);
// Number of the month (01-12) : 03
how = how.replace(/%zm/g, this.getMonth() + 1);
// Number of the day in the month (1-31)
how = how.replace(/%e/g, this.getDate());
// Number of the day in the month (01-31)
how = how.replace(/%d/g, this.getDate());
// Time (24 hours format, 00-23)
how = how.replace(/%H/g, this.getHours());
// Time (12 hours format, 00-12)
if(this.getHours() > 12)
H = this.getHours() - 12 + 'pm';
else H = this.getHours() + 'am';
how = how.replace(/%I/g, H);
// Minutes
how = how.replace(/%M/g, this.getMinutes());
// Seconds
how = how.replace(/%S/g, this.getSeconds());
// Year, 2 digits (00-99)
year = this.getFullYear() + '';
year = year.substr(2, 4);
how = how.replace(/%y/g, year);
// Year, 4 digits
how = how.replace(/%Y/g, this.getFullYear());
// Number of the century (year divided by 100 and rounded between 00 and 99)
how = how.replace(/%C/g, parseInt(this.getFullYear() / 100));
}
});