Javascript Date.toString() formatting?

88,650

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

Share:
88,650

Related videos on Youtube

user1107685
Author by

user1107685

Updated on March 09, 2020

Comments

  • user1107685
    user1107685 about 4 years

    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>
    
    • Matt Zeunert
      Matt Zeunert over 11 years
    • user1107685
      user1107685 over 11 years
      Thanks Mat - I was able to put together a solution from the answers on that. I've edited the question with a solution.
    • Alex
      Alex almost 8 years
      Please put your answer in an answer instead of the question.
  • k0pernikus
    k0pernikus almost 10 years
    @user1107685 Please do not add answers within questions. Just add your own answer instead.
  • Varun Sharma
    Varun Sharma almost 7 years
    If there are plenty of libraries then naming a few shouldn't have hurt you...
  • Sandra
    Sandra about 5 years
    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 stackoverflow.com/a/10685571/1875965