AngularJS在ng-model中获取格式化日期

red*_*rom 21 javascript date angularjs

我在时间戳中按照模型值填充了以下文本输入:

<input type="datetime" ng-model="workerDetail.dateOfBirth"  class="form-control" id="date_of_birth" />
Run Code Online (Sandbox Code Playgroud)

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

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

我试着这样做:

{{workerDetail.dateOfBirth | date:'MMM'}}
Run Code Online (Sandbox Code Playgroud)

但没有运气.

谢谢你的建议.

bla*_*ind 30

你可以试试一个过滤器

HTML

<input type="datetime" ng-model="mydateOfBirth"  class="form-control" id="date_of_birth" />
Run Code Online (Sandbox Code Playgroud)

控制器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'); 
});
Run Code Online (Sandbox Code Playgroud)


Ser*_*lto 5

您还可以指定如下所示的时区:

$scope.$watch('workerDetail.dateOfBirth', function (newDate) {
    $scope.workerDetail.dateOfBirth = $filter('date')(newDate, 'HH:mm', 'UTC'); 
});
Run Code Online (Sandbox Code Playgroud)