Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting what is the first day of the week based on Locale with momentJs

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!

like image 268
Mario Levrero Avatar asked Oct 30 '14 16:10

Mario Levrero


3 Answers

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/

like image 166
motig88 Avatar answered Nov 17 '22 08:11

motig88


Looks to me that you wanted to get a locale aware start of week (startOf('week')) and return its value as an isoWeekday, not the date or anything? In that case your question is the answer.

moment().startOf('week').isoWeekday();
like image 4
durrrr Avatar answered Nov 17 '22 09:11

durrrr


It looks like you can just do moment().locale('us').weekday(0) as of version 2.1.0 http://momentjs.com/docs/#/get-set/weekday/

like image 1
mayorbyrne Avatar answered Nov 17 '22 08:11

mayorbyrne