Search code examples
javascriptangularjsbootstrap-datepickerangular-bootstrap

AngularJs Bootstrap Datepicker Popup custom date onload


I am using AngularJs Bootstrap datepicker and what I'm trying to do is to add a date which I have in a variable.

Here is the url: Datepicker Homepage

So for example:

var myDate = '2017-12-12';

Then I have this:

$scope.today = function() {
  $scope.dt = new Date();
}

The code about is adding today's current date and I want to add the value of the variable to the datepicker input.

So I've tried:

$scope.dt = new Date(myDate);

But this is not populating the datepicker input with the new date.

So my question is...How do I get this done?


Solution

  • The Date constructor does not know how to convert your string to a proper date object.

    Try it like this

    var myDate = '2017-12-12';
    var dtArray = myDate.split("-");
    $scope.dt = new Date(dtArray[0], dtArray[1] - 1, dtArray[2]); 
    

    NOTE: The month is counted from 0-11, thats why we need to add minus one