Javascript Date.toString() formatting?

user1107685 picture user1107685 · Sep 5, 2012 · Viewed 85.9k times · Source

Possible Duplicate:
Formatting a date in JavaScript

I have the following piece of script. It's a HTML5 slider with a date range. The slider is using a unix timestamp and I want to display the current selection in a readable format.

This is working fine but is outputting as "Wed May 16 2012 08:07:30 GMT+0100 (GMT Daylight Time)" despite me specifying the format as "yyyy-MM-dd HH:mm:ss".

Any ideas why it's not outputting in my format?

<input id="slider3" type="range" min="1337149800" max="1337160600"
  step="450" onchange="printValue('slider3','rangeValue3')"/>
<input id="rangeValue3" type="text" size="90"/>

<script>
function printValue(sliderID, textbox) {
  var x = document.getElementById(textbox);
  var y = document.getElementById(sliderID);

  var d1=new Date(y.value*1000);

  var newtimestamp = d1.toString("yyyy-MM-dd HH:mm:ss");

  x.value = newtimestamp;
}
</script>

Answer

Daniel A. White picture Daniel A. White · Sep 5, 2012

JavaScript's Date object does not support that. There's plenty of libraries to do this for you.