Jquery Slider Month/Year

前端 未结 1 2015
长情又很酷
长情又很酷 2021-01-28 03:29

I have some code and it is currently displaying the date in the following format on the slider : Tue Jan 01 2013 - Wed Jan 01 2014, However, I am wanting it to display in the fo

相关标签:
1条回答
  • 2021-01-28 03:59

    The new Date() has invalid date time format, change it to 'yyyy-mm-ddThh:min:ss', then it should work.

    Check JavaScript Date to figure out how to construct one Date object.

    $(function() {
        $( "#slider-range" ).slider({
          range: true,
          min: new Date('2012-01-01T00:00:00').getTime() / 1000,
          max: new Date('2019-01-01T00:00:00').getTime() / 1000,
          step: 864,
          values: [ new Date('2012-03-01T00:00:00').getTime() / 1000, new Date('2014-01-01T00:00:00').getTime() / 1000 ],
          slide: function( event, ui ) {
            $( "#amount" ).val( (new Date(ui.values[ 0 ] *1000).toDateString() ) + " - " + (new Date(ui.values[ 1 ] *1000)).toDateString() );
          }
        });
        $( "#amount" ).val( (new Date($( "#slider-range" ).slider( "values", 0 )*1000).toDateString()) +
          " - " + (new Date($( "#slider-range" ).slider( "values", 1 )*1000)).toDateString());
    
      });
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      
      <p>
      <label for="amount">date range:</label>
      <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;width:400px;">
    </p>
     
    <div id="slider-range"></div>

    Update: below is the simple month/year ranger, The primary modifications are adjust some description texts and the date format.

    function formatDate(date) {
      var monthNames = [
        "January", "February", "March",
        "April", "May", "June", "July",
        "August", "September", "October",
        "November", "December"
      ];
    
      var monthIndex = date.getMonth();
      var year = date.getFullYear();
    
      return monthNames[monthIndex] + ' ' + year;
    }
    
    $(function() {
        $( "#slider-range" ).slider({
          range: true,
          min: new Date('2012-01-01T00:00:00').getTime(),
          max: new Date('2019-01-01T00:00:00').getTime(),
          step: 86400000,
          values: [ new Date('2012-03-01T00:00:00').getTime(), new Date('2014-01-01T00:00:00').getTime() ],
          slide: function( event, ui ) {
            $( "#amount" ).val( formatDate(new Date(ui.values[0])) + '-' + formatDate(new Date(ui.values[1])) );
          }
        });
        $( "#amount" ).val( formatDate((new Date($( "#slider-range" ).slider( "values", 0 )))) +
          " - " + formatDate((new Date($( "#slider-range" ).slider( "values", 1 )))));
    
      });
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      
      <p>
      <label for="amount">month range:</label>
      <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;width:400px;">
    </p>
     
    <div id="slider-range"></div>

    0 讨论(0)
提交回复
热议问题