3

I'm trying to get a date for flights from the user and the datepicker always sets the date as datetime object with the user's timezone, resulting in a wrong date sent to server. tried to use ng-model-options="{ timezone: 'utc' }"

but then the user see the wrong date after choosing the date (the date displayed as a day before what the user chose).

How can I tell datepicker to completely ignore timezone - if not possible what can I do on FE / BE to convert this date to non timezone date ?

I know I can convert it to Posix on FE but once I do it the datepicker screams that it needs date object.

Thanks in advance...

Rakesh Chand
  • 3,105
  • 1
  • 20
  • 41
ransa
  • 41
  • 4
  • Take a look to this library: https://momentjs.com/ it's used to manage dates in JS (you can use in FE and in BE with node), see the api doc, you can convert timezones and whatever you want, i hope it will be helpful to you – Kalamarico Sep 06 '17 at 11:14

1 Answers1

0

I had exactly same issue and I ended us using this code

Date.prototype.toJSON = function () {
   var timezoneOffsetInHours = -(this.getTimezoneOffset() / 60); //UTC minus local time
   var sign = timezoneOffsetInHours >= 0 ? '+' : '-';
   var leadingZero = (timezoneOffsetInHours < 10) ? '0' : '';

   //It's a bit unfortunate that we need to construct a new Date instance 
   //(we don't want _this_ Date instance to be modified)
   var correctedDate = new Date(this.getFullYear(), this.getMonth(), 
   this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), 
   this.getMilliseconds());
   correctedDate.setHours(this.getHours() + timezoneOffsetInHours);
   var iso = correctedDate.toISOString().replace('Z', '');

   return iso + sign + leadingZero + Math.abs(timezoneOffsetInHours).toString() + ':00';
}

its from this other SO question How to JSON stringify a javascript Date and preserve timezone

vmachacek
  • 530
  • 1
  • 11
  • 27