【问题标题】:Javascript date range slider is working in chrome but not in other browser?Javascript 日期范围滑块在 chrome 中工作,但在其他浏览器中不工作?
【发布时间】:2018-01-11 12:12:51
【问题描述】:

我有一个 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 等其他浏览器中不支持

【问题讨论】:

    标签: javascript jquery internet-explorer-8 mozilla date-range


    【解决方案1】:

    您的代码使用了日期字符串构造函数,这在浏览器之间是不一致的。 您应该检查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 !
    

    在下面检查您的代码 sn-p 相应更新(使用 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>

    【讨论】:

    • ** 需要更改:** 当前开始和结束日期显示为Tue Jan 01 2013 - Wed Jan 01 2014,但我只想显示Jan 2013 - Jan 2014
    • Date.toLocaleDateStringvar options = { year: 'numeric', month: 'short'}; 修改#amount 标签日期值。您也可以使用moment.js library 代替格式化日期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多