javascript - 如何更改输入日期格式?

标签 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 |日期:'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/

相关文章:

javascript - 在 Angular JS 中使用异步 POST 调用检索 CSV 数据后,如何强制浏览器显示文件下载对话框?

angularjs - 在表单上使用 ng-disabled 和 $invalid 时是否需要 ng-model?

javascript - Angular.js ng-repeat 回调

date - Doctrine 的日期字段——query 的写法

r - 将字符转换为日期的函数

javascript - 为什么这个 QUnit RegExp 测试失败了?

javascript - 手机版网站滑动功能

excel - dd/mm/yyyy 格式的日期在 Excel Vba 中返回 1004 错误

javascript - 将参数传递给 jQuery .click 同时保留事件参数

asp.net - 将生成的 javascript 从渲染的 html 中移出