Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

missing weeks 'ww' in the dateParser #4213

Closed
marcelboelen opened this issue Aug 17, 2015 · 3 comments
Closed

missing weeks 'ww' in the dateParser #4213

marcelboelen opened this issue Aug 17, 2015 · 3 comments

Comments

@marcelboelen
Copy link

When using weeks in the datepicker format there will be no change in the angular ngModel. (undefined). I debugged the code and found out that the dateParser can't parse a week 'ww'.

Currently i added my own week parser to the dateParser using the following

OLD


    'ww': {
      regex: '\\d{2}',
      apply: function(week) {
        // http://stackoverflow.com/a/16591175/1668675
        var year = (new Date()).getFullYear();
        if (typeof this.year !== 'undefined') {
          year = this.year;
        }
        var simple = new Date(year, 0, 1 + (week - 1) * 7);
        var dow = simple.getDay();
        var ISOweekStart = simple;
        if (dow <= 4) {
          ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
        } else {
          ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
        }
        this.date = ISOweekStart.getMilliseconds();
      }
    }

NEWEST

  • updated Regex to the regex @wesleycho suggested
  • updated date value setters
    
    'ww': {
      regex: '[0-4][0-9]|5[0-3]',
      apply: function(week) {
        // http://stackoverflow.com/a/16591175/1668675
        var year = (new Date()).getFullYear();
        if (typeof this.year !== 'undefined') {
          year = this.year;
        }
        var simple = new Date(year, 0, 1 + (week - 1) \* 7);
        var dayOfWeek= simple.getDay();
        var ISOweekStart = simple;
        if (dayOfWeek<= 4) {
          ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
        } else {
          ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
        }
        // Set the values individually based on the current week date
        this.year = ISOweekStart.getFullYear();
        this.month = ISOweekStart.getMonth();
        this.date = ISOweekStart.getDate();
        this.hours = ISOweekStart.getHours();
        this.minutes = ISOweekStart.getMinutes();
        this.seconds = ISOweekStart.getSeconds();
        this.milliseconds = ISOweekStart.getMilliseconds();
      }
    }
    
    

I used an solution already available on stackoverflow
http://stackoverflow.com/a/16591175/1668675
http://stackoverflow.com/questions/16590500/javascript-calculate-date-from-week-number

@wesleycho
Copy link
Contributor

That regex is not a good idea - it is too broad.

If you want to add support, you want '[0-4][0-9]|5[0-3]' as your regex string.

I took a look at this feature just now for the w and ww flags, and this might take some work to implement.

Anyone implementing this should use the weekGetter function for guidance perhaps from https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js#L290-L300 .

@marcelboelen
Copy link
Author

I totally agree with the '[0-4][0-9]|5[0-3]' as a better regex. The code above was just a quick fix for me.

It definitely requires some work to implement. The problem with the ww and w flag is that it's hard to use in combination with the other flags.

I'm looking forward to this feature.

@wesleycho
Copy link
Contributor

I am going to close this issue in favor of #3418 in order to decrease the issue clutter.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants