I am struggling to get the correct week in year for any given date.

Datetime dt = Datetime.newInstanceGmt(2014, 1, 5).addHours(12);
Integer week = Integer.valueOf(dt.formatGmt('w'));
System.debug('Locale: ' + UserInfo.getLocale());
System.debug('week number of ' + dt + ' is ' + week);
System.debug('first day of week is: ' + dt.dateGmt().toStartOfWeek());

January 5th this year was a Sunday, so I would expect it to return week number 1 for a European Locale such as 'de-DE' and week number 2 for a US Locale.

EDIT: I added another line to the script above. This last line shows me that the first day of the week containing the 5th of January 2014 is actually the previous Monday (30th December 2013). Which is correct for the Locale 'de-DE'! But the first Sunday of the year is in week 2? Looks really like a bug to me.

Unfortunately it returns tells me that the day is in week 2 for both Locales.

Is this a bug?


This has more to do with the ISO 8601 Date format than Salesforce. A week in a year starts on the first thursday in the calendar.

ISO 8601 Week Dates

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year (there is no week 00). 28 December is always in the last week of its year.


While the above is true, it doesn't apply to you. I see what you are saying now. It seems to work when formatting this way:

DateTime.newInstanceGmt(Date.newInstance(2014, 1, 5).toStartOfWeek(), Time.NewInstance(12,0,0,0)).format('w')

  • Ok, that's fine. 1st January 2014 was a Tuesday, so week 1 is the week from 30th December 2013 until - and including - 5th January 2014. Because in Germany - as in many other European countries - a week starts on a Monday end ends on its last day, a Sunday. Therefore, Sunday January 5th 2014 was the last day of week 1. – FrankZ Sep 14 '14 at 14:44
  • I edited the answer above. – ScottW Sep 14 '14 at 15:11
  • Thank you, Scott. That works. Though it still looks like a bug to me. – FrankZ Sep 14 '14 at 16:22


Sorry, I can't add a coment, so I will write an answer.

Your answer about ISO week number in Apex does not work when there are 53 weeks in the given year. Try the following code:

DateTime.newInstanceGmt(Date.newInstance(2015, 12, 28).toStartOfWeek(), 

It returns 1, instead of 53 (see: https://www.epochconverter.com/weeks/2015). Can something be done about that?


Because Sunday is the first day of the week, so 5th of January which is a sunday is the first day of the second week that is correct. Now here is what say Salesforce to config your first day of the week :

There is no explicit setting for the first day of the week in a user's calendar. Rather, this setting is controlled by changing the "Locale" on "My Personal Information" to a different English locale which recognizes Monday as the first day of the week for a calendar. For example, you can change you locale to "English (Ireland,Euro)", and afterwards should see the calendar display desired.

A user can get to this setting by following the clickpath below:

Go to Setup | My Personal Information | Personal Information | Edit and select a locale from the supported locale drop-down list.

A full list of supported locales can be found here: https://help.salesforce.com/apex/HTViewHelpDoc?id=admin_supported_locales.htm&language=en

  • As written, my Locale is set to Germany and here the first day of the week is Monday, as in most of Europe. – FrankZ Sep 14 '14 at 7:29

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.