| mikezilla.com

Format Date

I have been playing around with formatting the JavaScript date object and I have created two functions to help me out. The main function takes a date object and a format string and returns a formatted date. It is VERY basic at the moment but it works. The format string should be supplied in standard date format, in any order with any delimiters.

dd for Date of the month
MM for month
yy or yyyy for Year
hh for hours
mm for minutes
ss for seconds

e.g. dd/MM/yy hh:mm:ss

formatDate(DateObject, FormatString)

The function above uses my addZero function which takes a number converts it to a string and if it is less than 10 adds a "0" in front of it. i.e. 1 returns 01, 2 returns 02 etc.



Example
The order of the parameters can be changed or they can be removed altogether, you can also choose any "reasonable" delimeters.
Use Date Format:

© Mike Golding 2002