0

I have date with specific date format mentioned bellow.

Date_received = Tue Apr 05 2016 00:00:00 GMT+0530 (India Standard Time)

I'm getting this value using HTML5 input type="date" datepicker. I want to initialise the following 'min' attribute.

<input type="date" id="endDate" ng-model="user.endDate" min="" required />

I'm using

var startDate = new Date(Date_received).toISOString().split('T')[0]; var myEl = angular.element( document.querySelector( '#endDate' ) ); myEl.attr('min',startDate);

But this(new Date(Date_received).toISOString()) sets the value of startDate one day before the actual date. i.e. in this example it sets value of startDate = 2016-04-04T18:30:00.000Z

Kartik
  • 71
  • 1
  • 2
  • 11

2 Answers2

1

It's the same value, but toISOString() outputs your value for the UTC timezone.

http://www.timeanddate.com/worldclock/converted.html?iso=20160405T00&p1=54&p2=0

If you want to output the same format as the "ISO" but for the locale time zone, you should look at this solution:

How to ISO 8601 format a Date with Timezone Offset in JavaScript?

Community
  • 1
  • 1
Finickyflame
  • 853
  • 10
  • 12
0

Use Angular's $filter service. Inject it in your controller / directive

JS:

scope.startDate = startDateReceivedFromBackend;
scope.minDate = $filter('date')(scope.startDate, "dd/MM/yyyy");

See different formats here: https://docs.angularjs.org/api/ng/filter/date

HTML:

<input type="date" id="endDate" ng-model="user.endDate" min="minDate" required />
softvar
  • 17,917
  • 12
  • 55
  • 76
  • I used the following code and it worked `var minDate = $filter('date')($scope.user.startDate, "yyyy-MM-dd"); var myEl = angular.element( document.querySelector( '#endDate' ) ); myEl.attr('min',minDate);` Thanks – Kartik Apr 02 '16 at 20:09