Javascript does have a good Date object that can be used to manipulate dates in your script. Another benefit it sports is that it can take several string formats and create a Date object from that string. However, despite their efforts to use the most common strings you will likely come across a date format that doesn’t automatically convert over to the Date object.

This may be a little quick and dirty, but if you know your date string format is going to always be in a particular format, it is easy to convert that date string into a string that could be interpreted by the Date object constructor. Here is an example of such an effort:

1
2
3
4
5
6
7
8
9
10
11
<script>
  dateString = "09-Jan-10";
  var parts = dateString.match(/([0-9]*)\-([a-zA-Z]*)\-([0-9]*)/);
  dateString = parts[2] + " " + parts[1] + ", 20" + parts[3];
  var myDate = new Date(dateString);
  document.write("Date :" + myDate.getDate());
  document.write("<BR>");
  //getMonth is 0 based for some reason so add 1 to the result
  document.write("Month : " + myDate.getMonth()+1);
  document.write("<BR>");document.write("Year : " + myDate.getFullYear());
</script>