Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS get formatted date in ng-model

Tags:

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.

like image 462
redrom Avatar asked Oct 21 '14 12:10

redrom


People also ask

How to format date in ng model?

It is a simple HTML code where the ng-model value of your country format date is changed into some other country format date. Output: Example 2: In the following HTML code snippet, it is shown how to input date from the date picker menu and then express in a given format i.e. yyyy-mm-dd.

How do I change the input date format in HTML?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.


1 Answers

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');  }); 
like image 180
blackmind Avatar answered Oct 14 '22 19:10

blackmind