1. Home
  2. Computing & Technology
  3. JavaScript
Stephen Chapman
Stephen's JavaScript Blog

By Stephen Chapman, About.com Guide to JavaScript

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
Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Readers Respond
Nominate a Useful JavaScript
Add Your Response

Recent Blog Posts
Explore JavaScript
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2010 About.com, a part of The New York Times Company.

All rights reserved.