2

I'm using this code :

Time t = Time.newInstance(12, 0, 0, 0);
DateTime dt = DateTime.newInstance(Date.parse('22/02/2016'), t);
system.debug(LoggingLevel.WARN, Integer.valueof(dt.Format('w')));

It returns 9 but if I check in Salesforce calendar, the February 22nd corresponds to week 8.

Can someone explain me why ?

EDIT

It seems there is a bug with this method. I try the following code :

DateTime dt = DateTime.newInstance(2016, 1, 8);
String tz = 'Europe/Paris';
system.debug(LoggingLevel.WARN, Integer.valueof(dt.Format('w', tz)));

It returns 2 but in the calendar for this time zone (Salesforce calendar for example) the week number is 1.

I don't know what I can do with this !

1
  • 1
    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).So if you see 1 Jan 2016 is on friday so it will still be 52nd or 53rd week of previous year.
    – Jack vardy
    Jan 8, 2016 at 10:24

1 Answer 1

0

I guess this might be related to your timezone settings.

Look - I have faced similar issues before.

Look to my ideas:

https://success.salesforce.com/ideaView?id=08730000000DnP9AAK https://success.salesforce.com/ideaView?id=08730000000DnHPAA0

I guess you might need to use FormatGMT instead of Format method.

================

When I try to run your code under US-local settings, I get the error:

System.TypeException: Invalid date: 22/02/2016

Which means, you must have European local settings if your code works for you.

When I change your code to

Time t = Time.newInstance(12, 0, 0, 0);
DateTime dt = DateTime.newInstance(Date.parse('02/22/2016'), t);
//DateTime dt = DateTime.newInstance(Date.parse('22/02/2016'), t);
system.debug(LoggingLevel.ERROR, Integer.valueof(dt.Format('w')));

It returns me 9.

===================

I think it should be 9.

Look:

1 Jan 1-2

2 Jan 3-9

3 Jan 10-16

4 Jan 17-23

5 Jan 24-30

6 Jan 31 - Feb 6

7 Feb 7-13

8 Feb 14-20

9 Feb 21-27

10 28 - Mar 5

22 of Feb falls into 9th week.

==========================

Note that this really depends on your locale.

If I add additional debug monitoring dt itself

system.debug(LoggingLevel.ERROR, dt);

if would result into a different dates. Like

2016-02-22 17:00:00

on NY time or

2016-02-22 10:00:00

on Ukraine time, so if you go enough to some bias (like New Zealand +13 or Samoa -11) you might get some unexpected results.

10
  • Yes, but normally it will returns 8 ! Check SF calendar. This week is the 8th week of year. And it's the same with formatGMT().
    – SF_user
    Jan 8, 2016 at 10:30
  • are you referring to some standard SalesForce page? could you please make a screenshot or give a link to that standard page?
    – Patlatus
    Jan 8, 2016 at 10:34
  • It's in a custom page. But I try this code in Developer console to check why I didn't have the good week.
    – SF_user
    Jan 8, 2016 at 10:37
  • I think it should be 9. Look: 1 Jan 1-2 2 Jan 3-9 3 Jan 10-16 4 Jan 17-23 5 Jan 24-30 6 Jan 31 - Feb 6 7 Feb 7-13 8 Feb 14-20 9 Feb 21-27 10 28 - Mar 5 Could you please should the code which return 8?
    – Patlatus
    Jan 8, 2016 at 10:43
  • 2
    28 dec 2015 to 2 jan 2016 will be 1st week.So it will come as 9.
    – Jack vardy
    Jan 8, 2016 at 10:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .