AngularJS 在 ng-model 中获取格式化日期 [英] AngularJS get formatted date in ng-model

查看:30
本文介绍了AngularJS 在 ng-model 中获取格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下由时间戳中的模型值填充的文本输入:

I have following text input filled by model value in timestamp:

<input type="datetime" ng-model="workerDetail.dateOfBirth"  class="form-control" id="date_of_birth" />

它将输入中的值显示为给定的时间戳.

It displays value in input as given timestamp.

我想将输入中可见的值转换为格式化日期(YYYY/MM/DD),但在模型中应始终作为时间戳.

I would like to convert value which is visible in input into formatted date (YYYY/MM/DD), but in model should be always as timestamp.

我尝试这样做:

{{workerDetail.dateOfBirth | date:'MMM'}}

但没有运气.

感谢您的建议.

推荐答案

你可以试试过滤器

HTML

<input type="datetime" ng-model="mydateOfBirth"  class="form-control" id="date_of_birth" />

控制器JS

$scope.$watch('mydateOfBirth', function (newValue) {
    $scope.workerDetail.dateOfBirth = $filter('date')(newValue, 'yyyy/MM/dd'); 
});

$scope.$watch('workerDetail.dateOfBirth', function (newValue) {
    $scope.mydateOfBirth = $filter('date')(newValue, 'yyyy/MM/dd'); 
});

这篇关于AngularJS 在 ng-model 中获取格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆