Jquery Slider月/年

时间:2018-02-27 22:44:10

标签: javascript jquery

我有一些代码,它目前在滑块上显示以下格式的日期:2013年1月1日星期二 - 2014年1月1日星期三,但是,如果可能的话,我希望它以下列格式显示01/2015 - 01/2018我确保要改变什么才能让我实现这一目标。 感谢

$(function() {
    $( "#slider-range" ).slider({
      range: true,
      min: new Date('January 2012 00:0:00').getTime() / 1000,
      max: new Date('January 2019 00:0:00').getTime() / 1000,
      step: 86400,
      values: [ new Date('January 2013 00:0:00').getTime() / 1000, new Date('January 01, 2014 00:0: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());

  });

1 个答案:

答案 0 :(得分:0)

new Date()的日期时间格式无效,请将其更改为'yyyy-mm-ddThh:min:ss',然后才能生效。

检查JavaScript Date以弄清楚如何构造一个Date对象。

$(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>

更新:下面是简单的月/年游侠,主要修改是调整一些描述文字和日期格式。

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>