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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26486696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:57:26  来源:igfitidea点击:

AngularJS get formatted date in ng-model

javascriptangularjsdate

提问by redrom

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.

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

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.

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

I tried to do it by this way:

我试图通过这种方式做到这一点:

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

But without luck.

但没有运气。

Thanks for any advice.

感谢您的任何建议。

回答by blackmind

You can try a filter

你可以试试滤镜

HTML

HTML

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

Controller JS

控制器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'); 
});

回答by Seralto

You can also specify a timezone like this:

您还可以指定这样的时区:

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

回答by vignesh

Try This code to Get a date Only (dd/mm/yyyy)

尝试使用此代码仅获取日期 (dd/mm/yyyy)

html

html

<div class="col-lg-3">
<input type="date" name="dob" class="input-lg form-group" ng-model='dob'>
</div>

Angularjs

Angularjs

app.controller('myctrl',function($scope, $http,$window,$filter) {
$scope.sent=function(){
    $scope.date = $filter("date")($scope.dob, 'dd-MM-yyyy');
    alert($scope.date);
}})