如何在eonasdan bootstrap datetimepicker中获得dd / mm / yyyy到mm / dd / yyyy的值

时间:2016-05-20 03:08:46

标签: jquery datetimepicker bootstrap-datetimepicker eonasdan-datetimepicker

我想得到值输入(文本)标签:dd / mm / yyyy并将此值转换为:mm / dd / yyyy并发送服务器。

1 个答案:

答案 0 :(得分:0)

您可以使用date方法获取所选日期,然后使用当前format函数转换格式,以您需要的格式转换日期。 我建议您编辑您的问题并添加一些代码以获得更准确的答案,无论如何,以下示例可以帮助您完成工作:

$(function () {
  // Init Eonasdan datetimepicker
  $('#datetimepicker1').datetimepicker({format: 'DD-MM-YYYY'});
  $('#btnSend').click(function(){
    // Get selected date (moment object)
    var myDate = $('#datetimepicker1').data("DateTimePicker").date();
    if( myDate != null ){
      // Convert moment date to string chosing format
      var dateStringForServer = myDate.format('MM-DD-YYYY');
      alert('send: '+dateStringForServer);
    }
    
  });
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>

<div class='input-group date' id='datetimepicker1'>
  <input type='text' class="form-control" />
  <span class="input-group-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>
<button id="btnSend" class="btn btn-primary">Send</button>