Balu Krish
Balu Krish

Reputation: 53

How to assign today date by default

I want to add today date as value for text box. I have a directive for showing date in dd/mm/yyy(23/03/2017). Format. Direcive:-

app.directive('formatDateInModel', function($filter) {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ngModel) {
            ngModel.$formatters.push(function(value) {
                var returnDate = "";
                if (value) {
                    returnDate = $filter('date')(new Date(value), 'dd/MM/yyyy');
                }
                return returnDate;
            });
        }
    };
});

By default i need today date so I have return The following code to show today date. But it is showing invalid date.

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;
$scope.ng-model = today;

Upvotes: 1

Views: 119

Answers (3)

user7760518
user7760518

Reputation:

you can directly use the date with filter like below

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
 $scope.date = new Date();
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
   <input type="text" ng-model="date | date:'dd/MM/yyyy'"/>
{{date | date:'dd/MM/yyyy'}}
  </body>

</html>

Upvotes: 1

Jenny
Jenny

Reputation: 663

use filter in controller:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope,$filter) {
 $scope.date = new Date();
 $scope.date =$filter('date')($scope.date, "dd/MM/yyyy");
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
  <input type="text" ng-model="date"  />
  </body>

</html>

Upvotes: 0

A.A Noman
A.A Noman

Reputation: 5270

You can try this

  Date.prototype.today = function () { 
  return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+     this.getFullYear();
}

 var newDate = new Date();
 var datetime = newDate.today();
 document.write(datetime);

Upvotes: 0

Related Questions