22

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.

3 Answers 3

31

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'); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

In fact, this answer caused me a trouble in a more sophisticated example, so I went for Sergio Toledo's one and everything got fine.
5

You can also specify a timezone like this:

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.