Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the today's date in datepicker textbox in angularjs

Tags:

angularjs

I have two datepicker textbox From date and To Date... I need default in From date this month start date and To date today's day...

How to set default dates in Angularjs Datepicker textbox..?

like image 498
Rajesh kumar Avatar asked Sep 27 '13 05:09

Rajesh kumar


2 Answers

I am not sure what you mean by a datepicker textbox. Are you using a date input?

<input name="dateInput" type="date" ng-model="date"/>

If so in the controller you need to set the ng-model variable, in this case, $scope.date to the default date you want to use e.g. to use today's date

var d=new Date();
var year=d.getFullYear();
var month=d.getMonth()+1;
if (month<10){
month="0" + month;
};
var day=d.getDate();
$scope.date=year + "-" + month + "-" + day;

I have set up a plunkr at http://plnkr.co/edit/WJ86aB?p=preview

like image 123
user2789093 Avatar answered Nov 14 '22 21:11

user2789093


If so in the controller you need to set the ng-model variable, in this case, $scope.date to the default date you want to use e.g. to use today's date

$scope.date = new Date();

In your html:

<input name="dateInput" type="date" ng-model="date"/>
like image 20
Klapsa2503 Avatar answered Nov 14 '22 22:11

Klapsa2503