26

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>
user1107685
  • 441
  • 1
  • 4
  • 13

1 Answers1

4

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 8
    @user1107685 Please do not add answers within questions. Just add your own answer instead. – k0pernikus Jul 08 '14 at 15:04
  • 21
    If there are plenty of libraries then naming a few shouldn't have hurt you... – Varun Sharma Jun 23 '17 at 22:32
  • 1
    recent implementations of JavaScript/ECMAScript allow `Date.toString()` and other variants. libraries like Moment.js help with dates in general, as not all days have 24 hours etc. see also https://stackoverflow.com/a/10685571/1875965 – Sandra Feb 25 '19 at 16:46