Date Format Method
Tuesday November 25, 2008
JavaScript provide the basic methods for extracting the parts of a date and time.
Unlike other languages JavaScript does not provide a nice simple way of converting dates into the nice display format that we require. Instead the usual way to build display dates in the format we want involves retrieving each part of the date separately and assembling them to put the date into the format we want. By adding a new method to our dates we can rectify this.
PHP allows dates to be easily formatted using the date function and supplying both the date and a string of characters all of which represent the way that we want parts of the date to display (for example: date("Y-m-d H:i:s",$start_date)). Adding my format method allows you to use the same formatting commands in your JavaScript with the JavaScript equivalent of that PHP call becoming start_date.format("Y-m-d H:i:s");. Differences between the date objects in the two languages mean that not all of the information that can be retrieved in PHP can be retrieved in JavaScript but my format method does support 26 of the most common formatting options.
Date Format Method


No comments yet. Leave a Comment