Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moment.weekdays() is localized, but order is not ISO #4579

Closed
d-damien opened this issue Apr 19, 2018 · 6 comments
Closed

moment.weekdays() is localized, but order is not ISO #4579

d-damien opened this issue Apr 19, 2018 · 6 comments

Comments

@d-damien
Copy link

d-damien commented Apr 19, 2018

It has been localized in #3284, but the order is still not ISO. I guess since the order is localized IRL, we might as well keep moment.weekdays() that way, but it would be nice to have moment.isoWeekdays(), that would be both ISO and localized − same syntax as moment().isoWeekday().

@d-damien
Copy link
Author

I imagine we also need moment.isoMonths(), as I believe year does not start on January in all countries. Not sure how this goes with moment.months().

@ashsearle
Copy link
Contributor

ashsearle commented Apr 19, 2018

How useful would it be for the order to be ISO?

JS arrays start at 0, not 1. The current implementation is usable using:

var m = moment(...);
var nameFromDay = moment.weekdays()[m.day()]; 
var nameFromIsoWeekday = moment.weekdays()[m.isoWeekday() % 7];

I'm not sure exactly what you're proposing, but if you're after an array returning ['Monday', 'Tuesday', ...] then that doesn't make things any easier:

var m = moment(...);
var nameFromDay = moment.isoWeekdays()[(m.day() || 7) - 1];
var nameFromIsoWeekday = moment.isoWeekdays()[m.isoWeekday() - 1];

@d-damien
Copy link
Author

I should have provided a use case, you are right. My use case is that I sort data according to their weekday, in a context where (despite english conventions and language), ISO weeks are used. Like :

let weekdays = moment.weekdays()
weekdays.push(weekdays.shift())
let sortFunction = d => weekdays.indexOf(d)

I have to add line 2 for going from english weeks to ISO weeks, and I feared that in case of multilingual applications, that would require a whole lot of checking for how different languages order weekdays (the kind of thing momentjs helps you to avoid).

@ashsearle
Copy link
Contributor

ashsearle commented Apr 20, 2018

You can remove line 2 if you make line 3 more complicated:

let sortFunction = d => (weekdays.indexOf(d) || 7)

@ashsearle
Copy link
Contributor

A couple more thoughts: I assume there's a reason you're sorting by weekday name (e.g. you don't have a date.) If you need to handle abbreviations too, you should look at moment.localeData().weekdaysParse(nameOfWeekday) - as that handles the abbreviated versions too - case-insensitively: e.g. Th, Thu, Thursday.

Regarding your suggestion about isoMonths() - do you know of any locale with a stable month ordering that doesn't start in January? (I mean: Chinese new year starts in either January or February, Islamic has started anywhere from September to December recently...)

Tax and financial years might be a good case where the months need to be ordered differently, but they don't map to a locale...

I think if you were going to add isoWeekdays, then you'd also have to add isoWeekdaysShort and isoWeekdaysMin for symmetry with weekdaysShort and weekdaysMin. Similarly, you couldn't really add isoMonths without also adding isoMonthsShort for symmetry with monthsShort

@d-damien
Copy link
Author

That would be a lot to add indeed. I think most of the time, people will stick to the locale's weekdays order, and not make a mess with ISO like we do (hopefully they will report it here if they don't).

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

No branches or pull requests

2 participants