blaa blaa -5 years ago 172
AngularJS Question

set current date on datepicker AngularJS

So, a modal opens up when I click on a button on m md-card to select a date. This is the code for it:

<!-- Modal -->
<md-dialog aria-label="Date Picker">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
<h4 class="modal-title" id="myModalLabel">{{modalTitle}}</h4>
<!-- /.modal-header -->
<div class="modal-body">
<md-input-container flex> <input type="date"
ng-model="startDateL"> </md-input-container>
<md-input-container flex> <label>{{}}</label> <input
type="date" ng-model="endDateL"> </md-input-container>
<!-- /.modal-search-box -->

<md-button ng-click="setDate()" class="md-primary">Update
<!-- /.modal-body -->
<!-- /.modal-content -->
<!-- /.modal-dialog -->
<!-- /.modal -->

and this is the code for my controller to select a date

function datePickerController($scope, $mdDialog, $rootScope, datePickerFactory, $filter){
console.log("suntem in date picker sel");
$scope.startDateL = new Date($rootScope.startDate);
$scope.endDateL = new Date($rootScope.endDate);

$scope.close = function() {

$scope.setDate = function(startDate, endDate) {
$rootScope.startDate = $scope.startDateL;
$rootScope.endDate = $scope.endDateL;

var stDate = $filter('date')($rootScope.startDate,'yyyy-MM-dd');
var enDate = $filter('date')($rootScope.endDate,'yyyy-MM-dd');

console.log(enDate);{sDate: stDate, eDate: enDate}, function(){

localStorage.setItem('chosenStartDate', JSON.stringify($rootScope.startDate));
localStorage.setItem('chosenEndDate', JSON.stringify($rootScope.endDate));



Now, what i want to do is to set a current date on my date field in my modal so that I have a default date when i open my modal.Is there a function that can set the current date?

Answer Source

You have to create a date object in your controller.

$scope.startDateL = new Date();
$scope.endDateL  = new Date();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download