【问题标题】:AngularJS Set Date Format to "MM/YYYY" After Enter the Date输入日期后,AngularJS 将日期格式设置为“MM/YYYY”
【发布时间】:2016-08-24 09:48:35
【问题描述】:

文本字段应该只允许数字,输入数字后,我应该将字段值转换为 DD/YY 格式。

JS:

app.controller('ctrlCreditPayment', function ($scope, $filter, $http, ngDialog,creditCard) {
$filter('cardexp')(new Date(), dd/yyyy);
});

HTML:

<input size="8" class="card-exp input-mid" placeholder="MM/YYYY" required type="text"  ng-model="cardexp">    

代码笔: https://codepen.io/fareed543-1472110535/pen/jAjGkq

【问题讨论】:

标签: angularjs date format


【解决方案1】:

试试这个
Js
$filter('date')(date, format, timezone)

$filter('cardexp')(new Date(), dd/yyyy, UTC)
注意:- 确保 'cardexp' 中有正确的数据

然后在ng-model中使用cardexp
喜欢-&lt;input size="8" class="card-exp input-mid" placeholder="MM/YYYY" required type="text" ng-model="cardexp"&gt;

您可以直接使用角度日期格式,请通过以下链接:-
https://docs.angularjs.org/api/ng/filter/date

【讨论】:

  • @MohammadFareed 我更新了我的答案可能会对你有所帮助。
  • 是的,显然它不起作用,因为您在ng-model 中添加了过滤器。不要使用` |日期:......, USE ng-model="cardexp"。您不能在ng-model 中添加过滤器,您只能在表达式中添加它,或者如果您不想在表达式中添加它,则在 JS 中使用 $filter
【解决方案2】:

我不认为这个答案是正确的,但我通过使用 ui-mask 得到了一个临时解决方案。允许数字并允许定义格式

<input type="text" ng-model="cardexp" ui-mask="9?9/9?9?9?9">

【讨论】:

    猜你喜欢
    • 2012-10-08
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多