bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Support ng-model-options with datepicker

Open wesleycho opened this issue 11 years ago • 27 comments

We should support ng-model-options with the datepicker

In particular, we should support

  • [x] allowInvalid
  • [x] updateOn
  • [x] timezone

wesleycho avatar Nov 07 '15 17:11 wesleycho

I would really like to set UTC timezone instead of local or without time/timezone when setting a date using the datepicker!

SandeepThomas avatar Nov 10 '15 14:11 SandeepThomas

:+1:

maxdeviant avatar Nov 11 '15 17:11 maxdeviant

@maxdeviant, please don't add +1 to issue threads without providing further benefit to the conversation. All it does is add clutter to the thread. Thanks.

We know this is something users want and fully intend to implement it. We have a lot on our plates at the moment trying to get 1.0 out the door.

icfantv avatar Nov 11 '15 17:11 icfantv

I should also add that we welcome well-thought and well-developed PRs if you really want/need this feature sooner rather than later.

icfantv avatar Nov 11 '15 17:11 icfantv

My apologies. It's a bit of a force of habit, at this point.

timezone support is what I am particularly interested in.

maxdeviant avatar Nov 11 '15 17:11 maxdeviant

I fixed my issue with timezone for now with this directive. Posting here in case any one needs.

https://gist.github.com/sandeepthomas/78f2de3f0f8f21869f91

It removes the timezone and converts the date to epoch/unix time.

SandeepThomas avatar Nov 19 '15 15:11 SandeepThomas

For firebase timestamps I am using this directive.

  .directive('timestamp', function($filter) {
    return {
      restrict: 'A',
      require: ['ngModel'],
      link: function link(scope, element, attr, ctrls) {
        var ngModelController = ctrls[0];
        var format = attr.uibDatepickerPopup;
        if (format) {
          ngModelController.$parsers.push(function(viewValue) {
            if (!viewValue) {
              return undefined;
            }
            // Strip off timezone offset
            if (angular.isFunction(viewValue.getTime)) {
              viewValue.setMinutes(viewValue.getMinutes() - viewValue.getTimezoneOffset());
              return viewValue.getTime();
            } else {
              return undefined;
            }
          });
          ngModelController.$formatters.push(function(modelValue) {
            if (!modelValue) {
              return undefined;
            }
            // Display the actual time, not local time
            return $filter('date')(modelValue, format, '+00:00');
          });
        }
      }
    };
  })

coryasilva avatar Nov 28 '15 00:11 coryasilva

@davious nice work sir, I just played with your timezone commit and I like it! There are a couple of issues I noticed: the calendar is ahead by one day, and the time moves ahead by one hour when you select a DST date (i.e.: March 7 vs March 14)

image

DST problem - it changed the time from 8 to 9: image

paulflo150 avatar Dec 10 '15 20:12 paulflo150

I was working on the timezone issue locally as well, as it turns out to be quite tricky - I have it completely working for the inline datepicker, but not the one in the popup.

I also have to rejig the tests.

For anyone working on this issue - a new method should be added to the dateParser service that essentially copy/pastes this mechanism from Angular: https://github.com/angular/angular.js/blob/master/src/Angular.js#L1207-L1224

wesleycho avatar Dec 10 '15 20:12 wesleycho

@davious Want any help on this?

mjonas87 avatar Dec 18 '15 16:12 mjonas87

@mjonas87 The timezone work is committed onto master via @09098f8

davious avatar Dec 18 '15 17:12 davious

@paulflo150 according to http://stackoverflow.com/questions/16946002/javascript-time-zone-is-wrong-for-past-daylight-saving-time-transition-rules/16951442#16951442 there is a design flaw in ECMAScript that makes DST handling flaky. Perhaps this explains the issue you experienced.

tlk avatar Feb 02 '16 15:02 tlk

Regarding updateOn: blur support - I also can’t update past 0.14.x now as our date-pickers stop working. The scenario we’d like is to have the blur functionality on the input field but still want the field updated when the user makes a selection from the popup. At the moment the same ng-model-options get used for both and the popup selection no longer works.

Happy to look at a PR but not sure what the preferred approach would be. A couple of potential solutions:

  1. Would it make sense to always specify or add {updateOn: default} when constructing the popup? I’m struggling to think of a scenario where delaying date selection from the pop-up would be desirable. This might at least be a sensible interim solution before defining more comprehensive updateOn support.

  2. Add additional config option for popupModelOptions that will override (or merge) the options used when creating the uib-datepicker-popup-wrap. Currently the ngModelOptions provided in datepicker-options are only used if they are not already set elsewhere.

Either of those sound acceptable?

mlev avatar Feb 26 '16 10:02 mlev

I have a drop down containing various time zones. when i select a timezone, the datepicker should be updated automatically. Is it possible to adjust ui-datepicker for selected timezone

velvizhip avatar Feb 26 '16 10:02 velvizhip

I was just following up on @mlev 's idea (1) for supporting updateOn: blur - for me everything works as expected when i just remove the line in popup.js where ng-model-options are passed to popupEl. Probably there is a use-case for having ng-model-options on the popup, but i dont see one for specifying updateOn.

I'd be happy to provide a PR, if there are no further concerns about this approach. What do you think, @wesleycho?

czosel avatar Mar 23 '16 12:03 czosel

Yes, please! This is causing some frustrating issues for me right now. @czosel && @wesleycho

Aaron-Pool avatar Mar 28 '16 14:03 Aaron-Pool

The timezone issue is fixed. You can use:

ng-model-options="{timezone: 'utc'}"

To get a datepicker without timezone calculation.

wald-tq avatar Mar 30 '16 07:03 wald-tq

I'm not sure what would be desired exactly for updateOn - would need to investigate.

I welcome any PRs that make sense though with tests to make it clear what is desired.

wesleycho avatar Mar 30 '16 12:03 wesleycho

@wesleycho Alright, i'll try to come up with a PR containing some tests. If you find any use for updateOn, please let me know!

czosel avatar Mar 30 '16 13:03 czosel

To give an update, updateOn support should be fixed as of #5933 in master, to be released for 2.0.0. getterSetter is the last piece of the puzzle, and shouldn't be too bad to implement if anyone wants to take a stab at it.

wesleycho avatar May 28 '16 16:05 wesleycho