0

I have a LWC that displays a calendar, which calls to APEX and the class returns all the data needed to fill the calendar. By default, the data is returned where Sunday is the first day of the week. However, in some locales, the first day of the week is Monday or Saturday.

I know I can access the user's locale with UserInfo.getLocale(), but I don't know how to find which day starts the week. I could look it up in a table or create a custom metadatatype, but that seems tedious. I have to think there's some internal property like UserInfo.getLocale().getFirstDayOfTheWeek().

Any ideas?

3

Simply use the LWC I18N functionality:

import firstDayOfWeek from '@salesforce/i18n/firstDayOfWeek';
2

If you want to access via Apex, you should be able to use the Date.toStartOfWeek method.

Returns the start of the week for the Date that called the method, depending on the context user's locale.

Depending on how you wish to consume the result, you may want to format it to get the weekday. For exmample:

Date startOfThisWeek = Date.today().toStartOfWeek();
String firstDayOfWeek = Datetime.newInstance(
    startOfThisWeek, Time.newInstance(12, 0, 0, 0)
).format('EEEE');
1
  • Thanks, this solves it with Apex, but Phil's solution lets me grab it without even calling an Apex method. – PatMcClellan__c Jan 22 '20 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.