Wednesday, April 22, 2009

Output JavaScript date in human-readable format (Day Month DD YYYY HH:MM:SS PM)

You often need to display the user's current date and time on a website in a custom format. Unfortunately JavaScript doesn't have (many) date formatting functions built-in so you either have to use someone else's JS date formatting "library" or roll your own.

For simplicity I rolled my own. And since this is common thing that we all need to do from time to time I thought I would share my code in case you find it useful.

The Demo

(The format the date appears in is Day Month DD YYYY HH:MM:SS PM.)

The date and time right now is

The Code

1 comment:

Andy said...

This was perfect for a project of mine :) Thanks for sharing! Saved me a whole bunch of time.