简体   繁体   中英

change date format of datepicker

I'm creating a web app in angularJS with ASP.NET, here is my JavaScript of a datepicker :

$('#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);
  }
});

The code in ASPX is the following:

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

The current format is MM/dd/yyyy . This is how the date is showing:

02/08/2017

I need to change the date format to:

dd-MM-yyyy

The data which I want to see is:

08-02-2017

All the help is appreciated!

Just add format option of datepicker:

format: 'dd-mm-yyyy'

Please find below snippet for more information

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

The following code will work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM