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.

2
  • do u need to display the filtered date in textbox ? Commented Jan 21, 2015 at 9:35
  • @K.Toress yes i want that Commented Jan 21, 2015 at 9:44

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Toress i 've problem when i'm saving date my c# code saves wrong date not the default date
are u sending $scope.ModifiedDate value to the backend ?
Toress yes im doing that
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();
});

3 Comments

ok it does display now but nw i want to save it to a field so how to link the field with date i displayed
i want to display the date in a textbox and the save then date to my table
thank you very much for responding. Toress got it right its working
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

1 Comment

thank you very much for responding. Toress got it right its working

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.