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 |日期:'yyyy-MM-dd'}}
。但我需要在 ng-model 中使用 'yyyy-MM-dd' 格式,而不是使用日期过滤器。我该如何解决这个问题?
最佳答案
你可以这样试试:
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
关于javascript - 如何更改输入日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211005/