...">

HTML5 input date format with angular scope

I have HTML 5 input type="date".

<input type="date" required ng-model="nm.pick" id="dpicker ">

Then in my angular controller I assign a value

nm.pick = $filter("date")(Date.now(), 'dd/MM/yyyy');

This will now give the default date format for HTML5 yyyy-MM-dd.

But I need a date format - dd/MM/yyyy

  • I cannot change <input type="date">to <input type="text"in my HTML. If I changed, dateformat will work fine
  • Then I change the input date to text input in the controller

    $("#dpicker").attr("type", "text");
    nm.pick = $filter("date")(Date.now(), 'dd/MM/yyyy');
    

    which does not work.

Basically I want to change the dateformat to dd/MM/yyyyin the controller without changing the HTML.

What is the best possible way to achieve this?

+4
source share
2 answers

, , - momentjs. angular 1.x.x . , . :

nm.pick = moment(nm.pick).format('DD-MM-YYYY');

,

+4

type="date"

var app = angular.module("MyApp", []).controller("MyCtrl", function($scope, $filter) {
  $scope.nm = {};
  $scope.nm.pick = new Date($filter('date')(new Date(), "yyyy-MM-dd"));
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>


<body ng-app="MyApp" ng-controller="MyCtrl">
    <input type="date" required ng-model="nm.pick" id="dpicker ">
</body>
Hide result

, , moment.js, / .

+3

Source: https://habr.com/ru/post/1677911/


All Articles