0

I got a view in angularjs and I'm just trying to display the current date.

html

 <input type="text" class="form-control" ng-model="lis.ModifiedDate" id="ModifiedDate">

app.js

$scope.ModifiedDate = $filter("ModifiedDate")(Date.now(), 'yyyy-MM-dd');

Can you please assist me here i want to display default date and the save it on a table.

ninjaXnado
  • 271
  • 1
  • 7
  • 17

3 Answers3

2

Ok. What you're asking for is how to format the data from the ng-bind and also edit it. What you need to do is little known and called a formatter:

How to do two-way filtering in angular.js?

You need to define functions that both read and write the formatted date. That is actually easy, but Date() parses date formats pretty well. You cannot just use filter to do this though.

Alternatively, if you're happy with a long-date format in the field you CAN edit that, and it will be updated as a normal bound value. Pretty delicate though - see plunkr:

http://plnkr.co/edit/MPfGoNsG74rAV6UuvBKk?p=preview

  <body ng-controller="MainCtrl">
    <p>Edit the date in the input:</p>
    <input ng-model="boundDate">
    {{boundDate | date : 'yyyy-mm-dd'}}
  </body>

app.controller('MainCtrl', function($scope, $filter) {
  $scope.boundDate = new Date();
});
Community
  • 1
  • 1
Peter Ashwell
  • 4,292
  • 2
  • 18
  • 22
2

change the controller as,

 $scope.ModifiedDate = $filter("date")(Date.now(), 'yyyy-MM-dd');

dont forget to inject $filter in to the controller

and html, you need to bind the value to textbox using ModifiedDate not with lis.ModifiedDate.

<input type="text" class="form-control" ng-model="ModifiedDate" id="ModifiedDate">

here is a sample Plunker

Kalhan.Toress
  • 21,683
  • 8
  • 68
  • 92
2

Use moment js it can convert date as required format

$scope.formattedDate = moment(new Date()).format("YYYY-MM-DD");

In html you use directly {{formattedDate}} it displays date in yyyy-mm-dd format

Naresh Kumar
  • 276
  • 2
  • 10