In the grand scheme of things, outputting dates into nicely human readable formats in JavaScript is easy. But it's repetitive. Really, excrutiatingly repetitive.
As someone who codes a lot of PHP, I am a fan of it's date function which gives a short-hand way of formatting dates for output. Here is my port of that function to JavaScript in the form of a jQuery plugin.
If you've ever used PHP's date function, adopting my plugin equivalent should be straightforward. Simply call like so: $.PHPDate("l, jS F Y H:i:s T", myDate); where the first parameter is a string and the second is a valid date object. The string can be made up of the characters listed below which is hopefully faithful to the PHP original.
Click here to see a quick example of calling: $.PHPDate("l, jS F Y H:i:s T", new Date());
| "d" | Day of the month, 2 digits with leading zeros eg. 01 to 31 | |
| "D" | A textual representation of a day, three letters eg. Mon through Sun | |
| "j" | Day of the month without leading zeros eg. 1 to 31 | |
| "l" lowercase L | A full textual representation of the day of the week eg. Sunday through Saturday | |
| "N" | ISO-8601 numeric representation of the day of the week eg. 1 (for Monday) through 7 (for Sunday) | |
| "S" | English ordinal suffix for the day of the month, 2 characters eg. st, nd, rd or th. Works well with j | |
| "w" | Numeric representation of the day of the week eg. 0 (for Sunday) through 6 (for Saturday) | |
| "z" | The day of the year (starting from 0) eg. 0 through 365 |
| "W" | ISO-8601 week number of year, weeks starting on Monday eg. Example: 42 (the 42nd week in the year) |
| "F" | A full textual representation of a month, such as January or March eg. January through December | |
| "m" | Numeric representation of a month, with leading zeros eg. 01 through 12 | |
| "M" | A short textual representation of a month, three letters eg. Jan through Dec | |
| "n" | Numeric representation of a month, without leading zeros eg. 1 through 12 | |
| "t" | Number of days in the given month eg. 28 through 31 |
| "L" | Whether it's a leap year 1 if it is a leap year, 0 otherwise. | |
| "o" | ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. eg. 1999 or 2003 | |
| "Y" | A full numeric representation of a year, 4 digits eg. 1999 or 2003 | |
| "y" | A two digit representation of a year eg. 99 or 03 |
| "a" | Lowercase Ante meridiem and Post meridiem eg. am or pm | |
| "A" | Uppercase Ante meridiem and Post meridiem eg. AM or PM | |
| "B" | Swatch Internet time eg. 000 through 999 | |
| "g" | 12-hour format of an hour without leading zeros eg. 1 through 12 | |
| "G" | 24-hour format of an hour without leading zeros eg. 0 through 23 | |
| "h" | 12-hour format of an hour with leading zeros eg. 01 through 12 | |
| "H" | 24-hour format of an hour with leading zeros eg. 00 through 23 | |
| "i" | Minutes with leading zeros eg. 00 through 59 | |
| "s" | Seconds with leading zeros eg. 00 through 59 |
| "e" | Timezone identifier Sadly this cannot recreated using JavaScript. Unless anyone knows how? | |
| "I" uppercase i | Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise. | |
| "O" | Difference to Greenwich time (GMT) in hours eg. +0200 | |
| "P" | Difference to Greenwich time (GMT) with colon between hours and minutes eg. +02:00 | |
| "T" | Timezone setting of this machine eg. EST, MDT | |
| "Z" | Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. eg. -43200 through 43200 |
| "c" | ISO 8601 date eg. 2004-02-12T15:19:21+00:00 | |
| "r" | RFC 2822 formatted date eg. Thu, 21 Dec 2000 16:01:07 +0200 | |
| "U" | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) eg. a big integer |
You are welcome to download and use my plugin which is distributed under the MIT licence. If you're interested to see other jQuery work I've done, check out my JAWStats web analytics project.
Useful stuff:
Code Snippets:
Recommended reading: