Javascript Date Format Patch 0.9.11

I’ve been combing trough my github “portfolio”, and noticed a fork in my Javascript Date Format Patch project, poked around to see what’s been modified, and found out that the minified version did not work properly… I didn’t test it, but I did notice that I needed to fix some “minor” issues (missing braces, semicolons, etc., embarrassing, I know).

Not to my defense, but, I didn’t use JSLint to verify the code because I just absolutely hate don’t like it, and I consider it to be more trouble than use, due to it’s lack of (or hard to do) configuration options. On that topic, in the diff of the commit, I discovered that the forker used a tool called JSHint to verify the code. Remembering all of the good features of JSLint, and the ability to configure out some of the annoying options in this fork, I immediately loved it…

Enough about these two… Javascript Date Format Patch is now updated, nothing functional, nothing drastic, but now it should definitely work minified… Thanks Paul… I’ve even skipped a version (by the way, its 0.9.11) due to the null vs undefined issues with Javascript, but more on that in some future post…

For more information on how to use this javascript date formatting and parsing patch, visit the old Javascript Date Formatter post.

Javascript Date Formatter

There is little (actually there is absolutely no) support in Javascript for formatting Date objects in a different formats when converting them to string, and for parsing dates from strings in different formats.

I have created a little “patch” to fix this issue, and to add the formatting and formatted parsing abilities to the Javascript Date object. This patch will add the following functions:

  • Date.format(dateObject, format)
  • dateObject.toFormattedString(format)
  • Date.parseFormatted(value, format)
  • dateObject.fromFormattedString(value, format)

The patch is called Javascript Date Format Patch, and it’s available on my github profile. Javascript Date Format Patch is dual licensed under the MIT and GPL licenses.

Format Description

When converting Date object to string, you can use any of the following format expressions, combined with other characters to get the output result you wish.

Format expression   Result
d the day as number without a leading zero (1 to31)
dd the day as number with a leading zero (01 to 31)
ddd the abbreviated localized day name (e.g. ‘Mon’ to ‘Sun’)
dddd the long localized day name (e.g. ‘Monday’ to ‘Sunday’)
M the month as number without a leading zero (1-12)
MM the month as number with a leading zero (01-12)
MMM the abbreviated localized month name (e.g. ‘Jan’ to ‘Dec’)
MMMM the long localized month name (e.g. ‘January’ to ‘December’)
yy the year as two digit number (00 to 99)
yyyy the year as four digit number
h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H the hour without a leading zero (0 to 23, even with AM/PM display)
HH the hour with a leading zero (00 to 23, even with AM/PM display)
m the minute without a leading zero (0 to 59)
mm the minute with a leading zero (00 to 59)
s the second without a leading zero (0 to 59)
ss the second with a leading zero (00 to 59)
z the milliseconds without leading zeroes (0 to 999)
zzz the milliseconds with leading zeroes (000 to 999)
AP use AM/PM display. “AP” will be replaced by either “AM” or “PM”
A same as “AP”
ap use am/pm display. “ap” will be replaced by either “am” or “pm”
a same as “ap”

More on formatting and parsing dates in Javascript…