Demonstration Of Date Object Formatting Using Date Prototyping

Posted September 8, 2006 at 2:32 PM

Tags: Javascript / DHTML

If anyone is interested, I put together this little demo for a dude who posted a comment on my site. It demonstrates how to apply a date and time mask to a Javascript Date object using regular expressions. It creates two methods that are prototyped to the Javascript Date object and therefore are available to all dates:

  • Date.prototype.dateFormat()
  • Date.prototype.timeFormat()

Regular Expression Date Mask Demo

http://www.bennadel.com/resources/demo/2/

Comments (3)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

Thanks a ton for this. Really needed it for making my own masks on Unix timestamps.

Posted by James on Jan 2, 2008 at 11:49 PM


Hey, by the way there is what I would consider to be a "bug" in this. getMonth() is a zero based function, so months like January come back as 0 and December comes back as 11. It can be fixed like this:

"m": this.getMonth()+1,
"mm": ((this.getMonth()+1).toString().length == 1) ? ("0" + this.getMonth()+1) : this.getMonth()+1,

Posted by James on Jan 3, 2008 at 12:08 AM


@James,

Thanks for the update. It seems to be handled differently in every language, so I am hesitant to call anything a bug. However, a zero-based month system never feels natural. You just want to say FIRST month (as in 1st as in 1) of the year.

Posted by Ben Nadel on Jan 3, 2008 at 7:15 AM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting