function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
t_Christ_Chris 

DateTime Leap Year Issue

Hi All,

 

I had a client come back to me with a bug whereby a calendar I built for them was showing two 04/01/2012 days.  This was only happening in April, and only in 2012.

 

On further inspection it looks as though adding 1 day to a DateTime variable with a date 03/31/12 and a time of 00:00:00:00  has the same output as adding 2 days to the same variable!  And this only seems to happen on this day, in 2012.

 

if you run this in the dev console, you'll see what I mean:

 

DateTime dt = dateTime.newInstance(date.newInstance(2012, 03, 31), time.newInstance(00, 00, 00, 00));
system.debug(dt.addDays(1).format('EEE dd/MM'));

 

The USER_DEBUG output is 'SUN 01/04...

 

Try running, adding 2 days to the variable

 

DateTime dt = dateTime.newInstance(date.newInstance(2012, 03, 31), time.newInstance(00, 00, 00, 00));
system.debug(dt.addDays(2).format('EEE dd/MM'));

 The USER_DEBUG output is 'SUN 01/04'!

 

Try different days and the method works ok...  If you add an hour on to the Time element of the dateTime, this seems to fix it... but that doesn't work in my overall architecture.

 

I can probably find a work around, just wondered if this is a bug, or is there something else I should be considering?

 

Cheers

 

Chris

_Prasu__Prasu_

Hi Chris,

 

I tried both scenrios and got correct output.

  1. 15:11:28:047 USER_DEBUG [2]|DEBUG|Sun 01/04
  2. 15:11:28:048 USER_DEBUG [4]|DEBUG|Mon 02/04

Are you using custom calender?

 

t_Christ_Chris

Hi Prasanna

 

I have built a custom calendar relying on the code snippets posted earlier (although a simplified version)

 

That was odd that it worked for you, but not for me as I'd tried it in several orgs myself.  I wondered if it was the timezone, so switched to PST, and I get the following:

 

20:58:38:043 USER_DEBUG [2]|DEBUG|Mon 02/04

20:58:38:043 USER_DEBUG [4]|DEBUG|Sun 01/04

 

Switch back to AEDT (Australian Eastern Daylight Time) and I get the following.

 

20:58:01:038 USER_DEBUG [2]|DEBUG|Sun 01/04

20:58:01:039 USER_DEBUG [4]|DEBUG|Sun 01/04

 

So looks as though it is affected by timezone.  Surely this is a bug?

_Prasu__Prasu_

True, it seems to be a bug.

 

My user was set in IST(Indian Standard Time). It worked correctly.

I updated my user to AEDT and it gave wrong results.

 

Is it due to Daylight saving or something? But still it should work.

t_Christ_Chris

As per the title, I think it may be due to the leap year, but timezone may be a contributing factor.

 

I've logged a support case, so we'll see what SFDC have to day.

 

Cheers

 

Chris