Tempus-dominus: Each time you open a picker, the time will change (timezone + custom format)

Created on 3 Oct 2015  路  14Comments  路  Source: Eonasdan/tempus-dominus

In current timezone implemnetation in the method getMoment
https://github.com/Eonasdan/bootstrap-datetimepicker/blob/master/src/js/bootstrap-datetimepicker.js#L157

there is a code to compare configured timezone with browser timezone.
This is plainly wrong. if timezone is set on datepicker we should not consider browser timezone at all, and assume that provided date string is already in the configured timezone.

The current implementation leads to issues, when you have different timezone configured in the picker and in the browser. Each time you open a datepicker your time will change.

I fixed it by changing

if (tzEnabled) {
  currentZoneOffset = moment().tz(options.timeZone).utcOffset();
  incomingZoneOffset = moment(d, parseFormats, options.useStrict).utcOffset();
  if (incomingZoneOffset !== currentZoneOffset) {
    timeZoneIndicator = moment().tz(options.timeZone).format('Z');
    dateWithTimeZoneInfo = moment(d, parseFormats, options.useStrict).format('YYYY-MM-DD[T]HH:mm:ss') + timeZoneIndicator;
    returnMoment = moment(dateWithTimeZoneInfo, parseFormats, options.useStrict).tz(options.timeZone);
  } else {
     returnMoment = moment(d, parseFormats, options.useStrict).tz(options.timeZone);
   }
} else {

to

if (tzEnabled) {
 returnMoment = moment.tz(d, parseFormats, options.timeZone);
  } else {

this will also resolve issue: https://github.com/Eonasdan/bootstrap-datetimepicker/issues/1355

Most helpful comment

This update is still missing on the master branch, right? When will it be included?

All 14 comments

@tmotyl You saved my day, thank's!

+1 to fix this.

took me hours to find out what the problem was. who would have guessed that the timezones plugin was the cause.

Hi folks, great plugin.

I ran into similar timezone issues. Is this released yet? Also any example on how we're meant to be using timezones, please?

When I get a dp.onchange that date has a timezone of +00:00 event though I am in timezone +11:001

thanks

+1 timezone examples required

This update is still missing on the master branch, right? When will it be included?

+1. Any news on this?

I am glad to find this, it was driving me nuts. Thanks.

+1 for a release with this fix!

Any update on this?

It would be good to see this in the master branch

This appears to be working as of the latest release version, it's just not documented! Pass in timeZone (capital "Z") into the init options.

As @rchanou said this has been solved here a Fiddle
Even If you don't set the default date it will take the time of the specified timeZone
i.e:
timeZone: 'Europe/London'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

malhayek2014 picture malhayek2014  路  4Comments

hmaidasani picture hmaidasani  路  5Comments

justqyx picture justqyx  路  3Comments

sabique picture sabique  路  5Comments

miraclebg picture miraclebg  路  3Comments