0

How can I set the default date in the datetime-local field?

This is my HTML :

<div class="col-lg-12 photo-container" ng-repeat="photo in selectedPhotos track by $index" ng-if="add">
            <input id="myDate" type="datetime-local" name="date[]" ng-model="form.date[$index]">
            <img ng-src="{{photo}}" class="thumb">
        </div>

I tried to set the default date in the controller for ng-model, i.e. $scope.form.date[0] = $filter("date")(Date.now(), 'dd-MM-yyyy HH:mm'); but it doesnt work. So, How can I set the default date for each input?

The main point is that the date should be set for every $index

  • Possible duplicate of [HTML5 Input Type Date -- Default Value to Today?](https://stackoverflow.com/questions/6982692/html5-input-type-date-default-value-to-today). Basically, use the standard ISO format. – Heretic Monkey Jan 02 '18 at 15:41
  • This is not duplicate, because I must assign date to model with $index. I have a problem with assigning this. – Mateusz Kibiłko Jan 03 '18 at 08:48

1 Answers1

0

Its generally difficult to play with datetime-local, but you can do this to initialize the model values -

var myDate = new Date().toISOString();
    $scope.myDemo = myDate.substring(0,myDate.length-1);

check the working fiddle

madhur
  • 903
  • 1
  • 11
  • 20
  • Thanks for reply. I will check this answer tomorrow. But I have yet one ask. How do I assign data in the controller to my model from the view? My model is : ng-model="form.date[$index]" – Mateusz Kibiłko Jan 02 '18 at 19:03
  • Do a foreach loop on selectPhotos in js and set every model equal to myDate – madhur Jan 03 '18 at 12:20