Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rock (SOTR) 2010 (Amsterdam) with:

Demonstration Of Date Object Formatting Using Date Prototyping

By Ben Nadel on

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/


Looking For A New Job?

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

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

Reply to this Comment

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,

Reply to this Comment

@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.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.