Javascript 日期范围滑块在 chrome 中有效,但在其他浏览器中无效?

Javascript date range slider is working in chrome but not in other browser?

我有一个 javascript 日期范围滑块代码,它在 Chrome 浏览器中工作正常,但在其他浏览器中不工作。

这是代码:

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  <script>
  $(function() {
    $( "#slider-range" ).slider({
      range: true,
      min: new Date('2012.01.01').getTime() / 1000,
      max: new Date('2019.01.01').getTime() / 1000,
      step: 86400,
      values: [ new Date('2013.01.01').getTime() / 1000, new Date('2014.01.01').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());

  });


  </script>

  <p>
  <label for="amount">Date range:</label>
  <input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" size="100"/>
</p>

<div id="slider-range"></div>

注意: 我认为,日期功能在其他浏览器中不支持,如 Firefox、IE8+、Safari

您的代码使用了 datestring 构造函数,这在浏览器之间是不一致的。 您应该检查 Date specification 以使用标准格式,例如:

new Date('December 17, 1995 03:24:00');

new Date(1995, 11, 17, 03, 24, 0, 0); // Beware of the month param starting at 0 !

检查下面相应更新的代码片段(使用 firefox 和 chrome 测试):

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  <script>
  $(function() {
    $( "#slider-range" ).slider({
      range: true,
      min: new Date('January 01, 2012 00:0:00').getTime() / 1000,
      max: new Date('January 01, 2019 00:0:00').getTime() / 1000,
      step: 86400,
      values: [ new Date('January 01, 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());

  });


  </script>

  <p>
  <label for="amount">Date range:</label>
  <input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" size="100"/>
</p>

<div id="slider-range"></div>