【问题标题】:change date format of datepicker更改日期选择器的日期格式
【发布时间】:2017-06-25 00:03:45
【问题描述】:

我正在使用 ASP.NET 在 angularJS 中创建一个 Web 应用程序,这是我的 datepicker 的 JavaScript:

$('#sandbox-container input').datepicker({
  autoclose: true
});
$('#sandbox-container input').on('show', function (e) {
  console.debug('show', e.date, $(this).data('stickyDate'));
  if (e.date) {
    $(this).data('stickyDate', e.date);
  } else {
    $(this).data('stickyDate', null);
  }
});
$('#sandbox-container input').on('hide', function (e) {
  console.debug('hide', e.date, $(this).data('stickyDate'));
  var stickyDate = $(this).data('stickyDate');
  if (!e.date && stickyDate) {
    console.debug('restore stickyDate', stickyDate);
    $(this).datepicker('setDate', stickyDate);
    $(this).data('stickyDate', null);
  }
});

ASPX 中的代码如下:

<div id="sandbox-container" class="row">
  <div class="col-lg-offset-4 col-md-offset-4 col-sm-offset-4 col-xs-offset-2">
    <div class="row">
      <input type="text" ng-model="datefrm" date-format="dd-MM-yyyy" class="textboxandbutton" placeholder="From" date-only />
    </div>
  </div>
  <div class="col-lg-offset-4 col-md-offset-4 col-sm-offset-4 col-xs-offset-2">
    <div class="row">
      <input type="text" ng-model="dateto" date-format="dd-MM-yyyy" class="textboxandbutton" placeholder="To" date-only>
    </div>
  </div>
</div>

当前格式为MM/dd/yyyy。这是日期的显示方式:

2017 年 2 月 8 日

我需要将日期格式更改为:

dd-MM-yyyy

我想看的数据是:

08-02-2017

感谢所有帮助!

【问题讨论】:

    标签: javascript asp.net angularjs date datepicker


    【解决方案1】:

    以下代码将起作用:

    var date = "01/24/1977";
    var datearray = date.split("/");
    var newdate = datearray[0] + '-' + datearray[1] + '-' + datearray[2];
    

    【讨论】:

      【解决方案2】:

      只需添加日期选择器的format 选项:

      format: 'dd-mm-yyyy'
      

      请在下面的 sn-p 中找到更多信息

      $('#sandbox-container input').datepicker({
          autoclose: true,
          format: 'dd-mm-yyyy'
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
      <link href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker3.css" rel="stylesheet"/>
      
      <div id="sandbox-container">
          <input type="text" type="text" class="form-control" />
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多