Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  140] [ 7]  / answers: 1 / hits: 20328  / 10 Years ago, thu, october 30, 2014, 12:00:00

Using momentJs, is possible to get the first day of the week (Monday(1), Sunday(7)...) based on Locale without creating a new moment?



I know I can access what is the first day of the week for the current locale with:



moment.locale('uk');
moment().startOf('week').isoWeekday(); //Returns 1
moment.locale('en');
moment().startOf('week').isoWeekday(); //Returns 7


But I think it's a bit ugly...




  1. Creating a momentjs object.

  2. Going to the first date of the week.

  3. Resolving the weekDay.



Any better idea?
Thx!


More From » momentjs

 Answers
14

This question has a proper answer in momentjs's current API:



moment.localeData('en-us').firstDayOfWeek();


As OP asked - no instance of moment() needed, also no ugliness of going to start of, just plain simple use of their localeData.



Note, that it might be required to download the moment+locale file which is significantly larger (44kb) than moment only (about 12kb).



Seems to be the case from version 2.2.0, more info can be found on their docs:
http://momentjs.com/docs/#/i18n/locale-data/


[#68958] Tuesday, October 28, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
braidenv

Total Points: 80
Total Questions: 104
Total Answers: 91

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;