restorando / angular-pickadate

A simple and fluid inline datepicker for AngularJS with no extra dependencies.
MIT License
273 stars 91 forks source link

timezone - selectedDate is one day before from modal date for other timezones #90

Open kishor-bhatt opened 7 years ago

kishor-bhatt commented 7 years ago

angular-pickadate works for my local time. To check global Times, I have changed my time zone to "America/Denver". Now selected date is taken one day before today's date (passed modal date), so it applies "pickadate-active" class to yesterday. I tried passing modal date with local timezone and also with UTC timezone. I don't know why dateHelper.parseDate calls again with stripping Timezone value earlier passed, now my understanding is $locale is converting stripped date assuming it a UTC date to local date. Hence, being GMT-06:00, selected date comes to one date before. HTML DIV - <div pickadate ng-model="vm.date" ng-model-options="{ debounce: 0 }" header="true" select="true" date-highlight-list="vm.dateList" ></div> Controller set value - vm.date = moment().tz(timeZoneName).format();

can someone suggest a way to handle different timezones with angular-pickadate?? Thanks !

kishor-bhatt commented 7 years ago

The parseDate function was giving date object as per GMT timezone, so date was becoming one day less.So I removed this condition which was directly returning GMT date for passed date strings with timezone. if (angular.isDate(dateString) || angular.isDate(new Date(dateString))) { new Date(dateString); }

Now it goes to next if block to format date with regex and added this condition there to handle date strings and date objects - if(typeof dateString == 'object') { dateString = (dateString['_d'])? dateString['_d']: dateString['_i']; // being private (_d, _i) should be avoided but only way in mine case if(typeof dateString == 'object') // returns Object by dateString['_d'] else string dateString = dateString.getDate() +'-'+ dateString.getMonth() +'-' +dateString.getFullYear(); }

dateParts = dateString.split(separator); // separator was "-" every time so making dateString with "-" in case it was object