redrom
redrom

Reputation: 11632

AngularJS get formatted date in 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.

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.

I tried to do it by this way:

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

But without luck.

Thanks for any advice.

Upvotes: 22

Views: 53727

Answers (3)

vignesh
vignesh

Reputation: 39

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

html

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

Angularjs

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

Upvotes: 2

blackmind
blackmind

Reputation: 1296

You can try a filter

HTML

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

Controller 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'); 
});

Upvotes: 31

Seralto
Seralto

Reputation: 1076

You can also specify a timezone like this:

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

Upvotes: 5

Related Questions