Ranger
Ranger

Reputation: 129

How to disable uib-timepicker converting time to Browser Timezone

In my Database time is storing in UTC and in frontend I'm using uib-timepicker to edit/update time. I don't want uib-timepicker to convert time from server timezone to local timezone. I want uib-timepicker to display time in UTC only.

So, How to stop uib-timepicker converting timezone?

P.S: I don't have any problem in IE. Only in Chrome, it is converting timezone.

Upvotes: 1

Views: 2557

Answers (2)

Betty St
Betty St

Reputation: 2860

Adding the following directive solves the issue for uib-timepicker using the uibDateParser from the uib-datepicker.

/**
 * UTC for timepicker
 */
angular.module('app').directive('uibTimepicker', function(uibDateParser) {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, ngModel) {

      ngModel.$formatters.push(function(value) { // view
        if(!value) { return value; }

        return uibDateParser.fromTimezone(value, 'UTC');
      });

      ngModel.$parsers.push(function(value) { // model
        if(!value) { return value; }

        return uibDateParser.toTimezone(value, 'UTC');
      });

    }
  };
});

Upvotes: 1

R.Costa
R.Costa

Reputation: 1393

Use the timezone offset to convert it back to UTC :

return new Date(date.getTime() + (60000 * date.getTimezoneOffset()));

Upvotes: 2

Related Questions