I got tired of always needing to write little functions to convert dates from strings to javascript Date objects and from Date objects to the string format I needed. The DateTime class uses a Date object as the backbone but makes it easy to convert between a variety of different date formats.
| dayOfWeek(format) | Returns the day of the week in the format given. 'd' returns 0 to 6, 'dd' returns 00 to 06, 'ddd' returns Sun to Sat, 'dddd' return Sunday to Saturday. |
| daysInMonth() | Returns the number of days in the current month of the Date() object. |
| getDate() | Returns the Javascript Date() object. |
| getAMPM(format) | Returns am/pm string. 'a' returns am or pm, 'aa' returns a.m. p.m., 'aaa' returns AM or PM, 'aaaa' returns A.M. P.M. |
| getDay(format) | Returns the day of the month according to the given format. 'dddd' returns Sunday to Saturday, 'ddd' returns Sun to Sat, 'dd' returns 01 to 31, 'd' returns 1 to 31. |
| getHour(format) | Returns the hour accourding to the given format. 'hhhh' returns 00 to 23, 'hhh' returns 0 to 23, 'hh' returns 01 to 12, 'h' returns 1 to 12. |
| getMilli(format) | Returns the millisecond according to the given format. 'zz' returns 000 to 999, 'z' returns 0 to 999. |
| getMinute(format) | Returns the minute according to the given format. 'ii' returns 00 to 59, 'i' returns 0 to 59. |
| getMonth(format) | Returns the month according to the given format. 'mmmm' returns January to December, 'mmm' returns Jan to Dec, 'mm' returns 01 to 12, 'm' returns 1 to 12. |
| getSecond(format) | Returns the second according to the given format. 'ss' returns 00 to 59, 's' returns 0 to 59. |
| getSuffix(format) | Returns the English suffix for the day according to the given format. 'x' returns st, nd, rd, or th. 'xx' returns ST, ND, RD, or TH |
| getYear(format) | Returns the year according to the given format. 'yyyy' returns a 4 digit year (1995, 2005, etc), 'yy' returns a 2 digit year (95, 05, etc). |
| incYear(value) | Icreases the year by the given value, negative values decrease. |
| incMonth(value) | Icreases the month by the given value, negative values decrease. |
| incDay(value) | Icreases the day by the given value, negative values decrease. |
| incHour(value) | Icreases the hour by the given value, negative values decrease. |
| incMinute(value) | Icreases the minute by the given value, negative values decrease. |
| incSecond(value) | Icreases the second by the given value, negative values decrease. |
| incMilli(value) | Icreases the millisecond by the given value, negative values decrease. |
| parse(dateStr, format) | Converts a date string into a Date() object, the format string must match the format of the date string |
| setYear(value) | Sets the year to the given value |
| setMonth(value) | Sets the month to the given value (1 = January, 12 = December, etc) |
| setDay(value) | Sets the day to the given value (between 1 and 31) |
| setHour(value) | Sets the hour to the given value (between 0 and 23) |
| setMinute(value) | Sets the minute to the given value (between 0 and 59) |
| setSecond(value) | Sets the second to the given value (between 0 and 59) |
| setMilli(value) | Sets the millisecond to the given value (between 0 and 999) |
| toString(format) | Returns the DateTime object as a string in the given format. If no format is given the format given when the DateTime object was created is used. |
| yy | Two Digit Year (05, 95, etc) |
| yyyy | Full Year (2005, 1995, etc) |
| m | Number of month from 1 to 12 (1 = January, 12 = December, etc) |
| mm | Zero padded number of month, from 01 to 12 (01 = January, 12 = December, etc) |
| mmm | Short name of month (Jan, Dec, etc) |
| mmmm | Full name of month (January, December, etc) |
| d | Number of day of month from 1 to 31 (1 = first day of month, etc) |
| dd | Zero padded number of day of month from 01 to 31 (01 = first day of month, etc) |
| ddd | Short name of day of week (Mon, Sun, etc) |
| dddd | Full name of day of week (Monday, Sunday, etc) |
| h | 12 based hour from 1 to 12 |
| hh | Zero padded 12 based hour from 01 to 12 |
| hhh | 24 based hour from 0 to 23 |
| hhhh | Zero padded 24 based hour from 00 to 23 |
| i | Minute from 0 to 59 |
| ii | Zero padded minute from 00 to 59 |
| s | Second from 0 to 59 |
| ss | Zero padded second from 00 to 59 |
| z | Millisecond from 0 to 999 |
| zz | Zero padded millisecond from 000 to 999 |
| a | am or pm |
| aa | a.m. or p.m. |
| aaa | AM or PM |
| aaaa | A.M. or P.M. |
| x | The lower-case suffix for the current day of the month (st, nd, rd, th) |
| xx | The upper-case suffix for the current day of the month (ST, ND, RD, TH) |
var dateTime = new DateTime('9:59 P.M.', 'hhh:i aaaa');
document.body.appendChild(document.createTextNode(dateTime.toString('hh:ii:ss:zz a')+'___'+dateTime.toString('hhh:ii:ss:zz')));
document.body.appendChild(document.createElement('br'));
var dateTime = new DateTime('0:10:9:0', 'hhh:i:s:z');
document.body.appendChild(document.createTextNode(dateTime.toString('hh:ii:ss:zz a')+'___'+dateTime.toString('hhh:ii:ss:zz')));
document.body.appendChild(document.createElement('br'));
var dateTime = new DateTime('12:47 P.M.', 'hh:ii aaaa');
document.body.appendChild(document.createTextNode(dateTime.toString('hh:ii:ss:zz a')+'___'+dateTime.toString('hhh:ii:ss:zz')));
document.body.appendChild(document.createElement('br'));
var dateTime = new DateTime('1:59 A.M.', 'h:i aaaa');
document.body.appendChild(document.createTextNode(dateTime.toString('hh:ii:ss:zz a')+'___'+dateTime.toString('hhh:ii:ss:zz')));
document.body.appendChild(document.createElement('br'));
var dateTime = new DateTime('23:59', 'hhhh:i');
document.body.appendChild(document.createTextNode(dateTime.toString('hh:ii:ss:zz a')+'___'+dateTime.toString('h:iia')));
document.body.appendChild(document.createElement('br'));
I hope this component is useful. If you find any bugs or have any suggestions or questions, please email me at josephhermens@gmail.com. Thanks, Joseph Hermens