« Back to the index

Date.prototype.* examples

The Date object has been enhanced with 1 main method to format the date:
format: will format the dates with a PHP-like Date syntax.
And some functions used to help this task:
setNames: used to adjust the language of the days and months.
isDate: check if a (year/month/day) vector is a valid date.
isTime: check if a (hour/min/sec) vector is a valid time.
isValid: check if a (year/month/day/hour/min/sec/ms) vector is a valid timestamp.
isLeapYear: check if the Date year is a leap year.
getOrdinalSuffix: get the ordinal suffix of the day of the month of the Date.
getMaxMonthDays: get the max days of the month of the Date.
getDayOfYear: get the day number in the year of the Date.
getWeekOfYear: get the number of the week day of the Date.
getGMTOffset: get the GMT offset of the Date.
getTimezone: get the Time zone of the Date.

setNames is a static method.
The methods isDate, isTime and isValid work on the Date object.
All the other methods work on the instances of Date.

Note: some original ideas were taken from PHP dates, algorithm from http://www.merlyn.demon.co.uk/weekcalc.htm and structures from extJS.


Let's put the date names in spanish:
function test1() { Date.setNames( ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'], ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'], ['Enero', 'Febrero', 'Mazo', 'Abril', 'Mayo', 'Junio', 'Julo', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'], ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'] ); } function test2() { Date.setNames( ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] ); }
Set the names in spanish Set the names in english

Let's check if some dates and times are valid:
function test3() { WA.get('#test3').text( '2009/01/31: ' + (Date.isDate(2009,1,31)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/02/31: ' + (Date.isDate(2009,2,31)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/02/30: ' + (Date.isDate(2009,2,30)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/02/29: ' + (Date.isDate(2009,2,29)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/02/28: ' + (Date.isDate(2009,2,28)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/04/31: ' + (Date.isDate(2009,4,31)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/04/30: ' + (Date.isDate(2009,4,30)?'Yes':'No') + '\n'); WA.get('#test3').append( '20:05:12: ' + (Date.isTime(20,05,12)?'Yes':'No') + '\n'); WA.get('#test3').append( '24:00:00: ' + (Date.isTime(24,0,0)?'Yes':'No') + '\n'); WA.get('#test3').append( '23:59:59: ' + (Date.isTime(23,59,59)?'Yes':'No') + '\n'); WA.get('#test3').append( '-1:10:10: ' + (Date.isTime(-1,10,10)?'Yes':'No') + '\n'); WA.get('#test3').append( '00:00:00: ' + (Date.isTime(0,0,0)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/01/01 12:00:00:000 : ' + (Date.isValid(2009,1,1,12,0,0,0)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/02/29 12:00:00:000 : ' + (Date.isValid(2009,2,29,12,0,0,0)?'Yes':'No') + '\n'); WA.get('#test3').append( '2009/02/28 23:59:59:000 : ' + (Date.isValid(2008,2,29,23,59,59,0)?'Yes':'No') + '\n'); }
Show the result

Let's work with today's date:
function test4() { var d = new Date(); WA.get('#test4').text( 'Today is: ' + d.format('j Y-m-d') + '\n\n'); WA.get('#test4').append( (d.isLeapYear()?'Is leap':'Is not leap') + '\n'); WA.get('#test4').append( 'This is the '+d.getDate()+d.getOrdinalSuffix() + ' day of the month.\n'); WA.get('#test4').append( 'There are '+d.getMaxMonthDays() + ' days in the month.\n'); WA.get('#test4').append( 'This is the day number ' + d.getDayOfYear() + ' of the year.\n'); WA.get('#test4').append( 'This is the week number '+d.getWeekOfYear() + ' in the year.\n'); WA.get('#test4').append( 'The time offset is ' + d.getGMTOffset() + '\n'); WA.get('#test4').append( 'The time zone is ' + d.getTimezone() + '\n'); WA.get('#test4').append( 'day: ' + d.format('j d D l') + '\n'); WA.get('#test4').append( 'day: ' + d.format('w N S z') + '\n'); WA.get('#test4').append( 'week: ' + d.format('W') + '\n'); WA.get('#test4').append( 'month: ' + d.format('n m M F t') + '\n'); WA.get('#test4').append( 'year: ' + d.format('L o y Y') + '\n'); WA.get('#test4').append( 'hour: ' + d.format('a A g G h H') + '\n'); WA.get('#test4').append( 'min, sec: ' + d.format('i s u') + '\n'); WA.get('#test4').append( 'zone: ' + d.format('O P T Z') + '\n'); WA.get('#test4').append( 'ISO: ' + d.format('c') + '\n'); WA.get('#test4').append( 'unix: ' + d.format('U') + '\n'); }
Show the result


It is highly recommended to replace the method getOrdinalSuffix with your own language method if you are going to use the ordinal representation of the day:
Example for spanish:
function test5() { Date.prototype.getOrdinalSuffix = function() { switch (this.getDate()) { case 1: case 21: case 31: return "er"; case 3: case 23: return "er"; default: return "o"; } } var d = new Date(); WA.get('#test5').append( 'Hoy es el '+d.getDate()+d.getOrdinalSuffix() + ' día del mes.\n'); }
Change the ordinal suffix calculation


« Back to the index