Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

calendar: doesn't handle timezone from ng-model-options #10431

Closed

Description

@simonered

Actual Behavior:

  • What is the issue? * The timezone is not considered: date is always returned with current timezone at midnight
  • What is the expected behavior?The return date is with selected timezone

CodePen (or steps to reproduce the issue): *

Angular Versions: *

  • Angular Version: 1.6.2
  • Angular Material Version: 1.1.3

Additional Information:

  • Browser Type: * Indifferent
  • Browser Version: *
  • OS: *
  • Stack Traces:

Activity

  1. changed the title [-]mdCaledar doesn't care of timezone[/-] [+]mdCalendar doesn't care of timezone[/+] on Feb 22, 2017
  2. rivertam commented on Mar 28, 2017

    @rivertam

    This is a huge issue for our application, and I don't know any workaround besides "don't use angular-md". I don't expect it to get fixed, though, considering the bug is probably considered something of a "feature" at this point. Would break old applications that rely on the bug...

  3. simonered commented on Mar 29, 2017

    @simonered
    Author

    I ended up writing a custom directive amending the returned date, to use within md-calendar. It's a good workaround (imho 😄 ), but I still think the bug has to be fixed.

    myApp.directive('tzDate', function($parse) {
    	  return {
    	    restrict: 'A',
    	    require: 'ngModel', 
    	    link: function (scope, element, attrs, ngModelCtrl) {
    	    	ngModelCtrl.$viewChangeListeners.push(function(){ 
    	    		if (ngModelCtrl.$modelValue && angular.isDate(ngModelCtrl.$modelValue)) {
    	    			var offset = attrs.tzDate;
    	    			offset && $parse(attrs.ngModel).assign(scope, moment(ngModelCtrl.$modelValue).utcOffset(offset, true).toDate());
    	    		}
    	    	});
    	    }
    	  };
    });
    <md-calendar ng-model="date" tz-date="+02:00"></md-calendar>
  4. rivertam commented on Mar 29, 2017

    @rivertam

    Nice! I ended up writing a component (that doesn't rely on moment, although I think moment is probably unnecessary with your approach as well)

    (function () {
      'use strict';
    
      angular.module('app.widgets').component('datePicker', {
        templateUrl: 'path/to/template.html',
        controller: DatePickerController,
        bindings: {
          minDate: '<?',
          maxDate: '<?',
          required: '<?',
          placeholder: '<?',
          currentView: '<?',
          name: '<?',
          initialDate: '<?',
          onChange: '&',
          ariaLabel: '<?'
        }
      });
    
      function DatePickerController() {
        var vm = this;
    
        if (!vm.initialDate) {
          vm.date = null;
        } else {
          // outer date is UTC
          // convert to local tim
          vm.date = new Date(vm.initialDate.getTime() + 60000 * vm.initialDate.getTimezoneOffset());
        }
        vm.update = update;
    
        function update(date) {
          // inner date is local time
          // convert to UTC to pass back up
          var utcDate = new Date(date.getTime() - 60000 * date.getTimezoneOffset());
          vm.onChange({ date: utcDate });
        }
      }
    }());
    <md-datepicker
      name="{{$ctrl.name}}"
      ng-required="$ctrl.required"
      md-min-date="$ctrl.minDate"
      md-max-date="$ctrl.maxDate"
      md-current-view="{{$ctrl.currentView}}"
      ng-model="$ctrl.date"
      layout
      md-placeholder="{{$ctrl.placeholder}}"
      ng-change="$ctrl.update($ctrl.date)"
      aria-label="$ctrl.ariaLabel"
    ></md-datepicker>
              <date-picker name="'someDate'" required="true" min-date="ctrl.minDate"
                             initial-date="ctrl.date"
                             on-change="ctrl.date = date"
                             placeholder="'Placeholder'"
                             ></date-picker>

    I'm a React fan so I reached for the component when I saw this problem.

  5. Splaktar commented on Jan 5, 2018

    @Splaktar
    Contributor

    I can see the difference in behavior and it needs to be fixed. I've updated the CodePen for 1.1.5 and the issue is still there.

  6. added this to the 1.1.6 milestone on Jan 5, 2018
  7. self-assigned this
    on Jan 5, 2018
  8. added
    i18n: localization
    needs: investigation
    P2: required
    on Jan 5, 2018
  9. changed the title [-]mdCalendar doesn't care of timezone[/-] [+]md-calendar: doesn't handle timezone from ng-model-options[/+] on Jan 5, 2018
  10. 23 remaining items

  11. gaby64 commented on Aug 26, 2019

    @gaby64

    any progress?

  12. gaby64 commented on Aug 27, 2019

    @gaby64

    @Splaktar

    DatePickerCtrl.prototype.onExternalChange = function(value) {
        var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
    
        this.date = !value ? value : new Date(this.locale.formatDate(value, timezone)); //changed part
    
        this.inputElement.value = this.locale.formatDate(value, timezone);
        this.mdInputContainer && this.mdInputContainer.setHasValue(!!value);
        this.resizeInputElement();
        this.updateErrorState();
      };

    works for me

  13. Splaktar commented on Aug 29, 2019

    @Splaktar
    Contributor

    @gaby64 I haven't had a chance to get back to this. I've been focused on P1 issues. If you want to submit a PR to fix this, I would be happy to review it and try to get it merged.

  14. added a commit that references this issue on Aug 29, 2019
  15. changed the title [-]md-calendar: doesn't handle timezone from ng-model-options[/-] [+]calendar: doesn't handle timezone from ng-model-options[/+] on Aug 31, 2019
  16. modified the milestones: g3: sync, 1.1.23 on Mar 6, 2020
  17. added
    has: Pull Request
    and removed on May 5, 2020
  18. added a commit that references this issue on May 12, 2020
    3f16d33
  19. Splaktar commented on May 12, 2020

    @Splaktar
    Contributor

    Opened PR 🧙‍♂️ #11906 ✨ for this issue.

  20. added a commit that references this issue on May 12, 2020
    ad2d394
  21. added a commit that references this issue on May 15, 2020
    2a01746
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2: requiredhas: Pull Requesti18n: localizationtype: enhancementux: integration

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions