如何更改输入日期格式?

时间:2015-10-19 09:29:18

标签: javascript angularjs date input

JS:

$scope.getDate = new Date();

HTML:

<div class="col-sm-4">
   <input type="date" ng-model="getDate" class="form-control input-sm">
</div>

结果:10/19/2015(在Chrome中)

很容易不使用ng-model。 {{getDate | date:'yyyy-MM-dd'}}。但是我需要&yyyy-MM-dd&#39; ng-model中的格式,不使用日期过滤器。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

你可以这样试试:

Date.prototype.myformat = function() {
        var yyyy = this.getFullYear().toString();
        var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
        var dd  = this.getDate().toString();
        return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
      };
var dd = new Date();
$scope.getDate = dd.myformat;

结果:2015-10-19

答案 1 :(得分:0)

JS:$scope.open = function($event) { $scope.status.opened = true; }; $scope.dateOptions = { formatYear: 'yy', startingDay: 1 }; $scope.format = 'yyyy-MM-dd'; var app = angular.module('someApp',['ui.bootstrap']); //app.js

HTML:<input type="text" class="form-control sm" ng-click="open($event)" datepicker-mode="month" datepicker-popup="{{format}}" ng-model="getDate" is-open="status.opened" datepicker-options="dateOptions" ng-required="true" current-text="Today" clear-text="Clear" close-text="Close"/>

result

感谢您的评论! ^^

答案 2 :(得分:0)

使用ng-bind将模型绑定到包含或不包含过滤器的其他HTML元素。

<p ng-bind=" getDate | date:'yyyy-MM-dd'"></p>
相关问题