indrimuska / angular-moment-picker

Angular Moment Picker is an AngularJS directive for date and time picker using Moment.js.
http://indrimuska.github.io/angular-moment-picker/
MIT License
527 stars 229 forks source link

Handling birthday - timezone issues #282

Open csimpi opened 5 years ago

csimpi commented 5 years ago

Hi, I have an issue with the timezones and I can't find the solution. When the user picks his/her birthday's date the angular-moment-picker is automatically converting the date to UTC format. This is not too lucky since when I'm sending the picked date to the backend the UTC date will be sent so I don't know what was the local timezone of the client. For example, if I'm in GMT and select my birthday: 1980-01-21 the backend will get 1980-01-20 as a date, and the wrong birthday will be stored (01-20 instead 01-21). When the users want to see other user's birthday a wrong date appears when a user is in different timezone from GMT, only those users are getting the correct value (01-21) who are in the same timezone like me (GMT), users with a browser in UTC will get the wrong 01-20 birthday date.

I have no idea how to handle this on the backend side ( I don't want to convert back and forth the picked date object on the client side that would be a bit ugly).

Can I somehow avoid converting the picked value to UTC automatically? Or any other suggestions how should I handle this?

CJDennis commented 3 years ago

The trick is to manually convert the moment object to UTC while not changing the display time:

var m = moment();
var keepLocalTime = true;

m.utc(keepLocalTime);

Now the value sent back to the back-end will still be in UTC (the default), but on the correct date.

For the issue of displaying values sent by the server, you might need to use m.local(keepLocalTime).