jquery - AngularJS UI Bootstrap Datepicker Controller 中的日期格式不正确

标签 jquery angularjs angular-ui-bootstrap bootstrap-datepicker bootstrap-datetimepicker

我在 html 页面中使用日期选择器,如下所示:

HTML

  <div ng-controller="StartDateCtrl">
    <label class="control-label" for="startDate">  Date:</label>   
    <div class="row">
        <div class="col-md-6">
            <p class="input-group">
              <input type="text"  name="startDate" class="form-control" show-button-bar="true" datepicker-popup="yyyy-MM-dd" ng-model="startDate" is-open="opened" min="minDate" max="'22-06-2015'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
              <span class="input-group-btn">
                <button class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
            </p>
        </div>
    </div>
    </div> 

日期选择器 Controller

var StartDateCtrl= function ($scope) {
  $scope.today = function() {
    $scope.travelStartDate = new Date();
  };
  $scope.today();

  $scope.showWeeks = true;
  $scope.toggleWeeks = function () {
    $scope.showWeeks = ! $scope.showWeeks;
  };

  $scope.clear = function () {
    $scope.travelStartDate = null;
  };

  // Disable weekend selection
  $scope.disabled = function(date, mode) {
    return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
  };

  $scope.toggleMin = function() {
    $scope.minDate = ( $scope.minDate ) ? null : new Date();
  };
  $scope.toggleMin();

  $scope.open = function($event) {
    $event.preventDefault();
    $event.stopPropagation();

    $scope.opened = true;
  };

  $scope.dateOptions = {
    'year-format': "'yy'",
    'starting-day': 1
  };

  //$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate'];
  $scope.formats = ['yyyy-MM-dd'];
  $scope.format = $scope.formats[0];
};

主 Controller

app.controller('UpdateShoppingBasketCtrl', ['$scope', 'ShoppingBasketFactory', '$location',
                                  function ($scope, ShoppingBasketFactory, $location) {


        alert('START DATE = '+ $scope.startDate );
}]);

提交网页后,我正在主表单 Controller 中检索日期,但格式与日期选择器弹出窗口中显示的格式不同。 选择后显示的格式例如为“26/03/2014”。 但是,在我的 Controller 中我得到: 2014 年 3 月 26 日星期四 00:00:00 GMT 0000(GMT 标准时间)

请问,如何获取 Controller 中显示的日期格式而不是“Thu Mar 26 2014 00:00:00 GMT 0000(GMT 标准时间)”???

最佳答案

您可以使用date用于格式化日期的过滤器。 这是demo :

<body ng-app="MyApp" ng-controller="MyController">
   <div>{{date}}</div>
</body>

脚本:

angular.module('MyApp',[])
       .controller('MyController',function($scope,$filter){
    $scope.date = $filter('date')(Date.now(),'yyyy-MM-dd'); 
});

关于jquery - AngularJS UI Bootstrap Datepicker Controller 中的日期格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22660191/

相关文章:

javascript - 验证后如何保留表单值

AngularJS - 修改 iframe DOM

css - 让 col-md-4 float ,col-md-8 滚动

javascript - 如何防止 jQuery-UI 按钮集破坏 AngularJS 绑定(bind)?

javascript - UI Bootstrap 破坏 angularjs post 请求

angularjs - Angular ui-bootstrap选项卡不呈现ui-view

jquery - 文章中的内联图像和标题 - 使标题的宽度与图像的宽度一致

javascript - Jquery 比较选中的数据/对象差异

javascript - jQuery 下一个-上一个图库按钮在结束时不停止

javascript - AngularJS 赋值后变量未定义