2

So I'm having an issue in Apex with the Date's addDays function. It seems that adding more than a certain amount of days throws off the time by one hour. It all seems so arbitrary so I'm wondering if this is a known thing in Salesforce or not.

DateTime aDate = System.now();
System.debug(aDate);
DateTime adjustedDate = 
DateTime.newInstance(aDate.date().addDays(4), aDate.time());

System.debug(adjustedDate);

Running that in an execute anonymous box outputs the following:
2019-03-05 17:44:28
2019-03-09 17:44:28

That's all good. The same date separated by four days and the same time. Just as you'd expect. But... adding five or more days causes behavior I can't wrap my head around.

DateTime aDate = System.now();
System.debug(aDate);
DateTime adjustedDate = 
DateTime.newInstance(aDate.date().addDays(5), aDate.time());
System.debug(adjustedDate);

This outputs the following:
2019-03-05 17:46:08
2019-03-10 16:46:08

It falls back by an hour. Any idea why or is this a known thing? I'm in central timezone so my time is actually -6 hours from the printed time.

0

1 Answer 1

4

Daylight saving time 2019 in central timezone will begin at

2:00 AM on Sunday, March 10

and ends at

2:00 AM on Sunday, November 3

All times are in Central Time.

:)

3
  • In other words, Daylight Saving Time begins in 5 days at this point in time. Which explains the discrepancy between adding 4 days and 5 days.
    – Derek F
    Commented Mar 5, 2019 at 18:11
  • Wow. I completely forgot about Daylight savings time. Thank you!
    – Dylan
    Commented Mar 5, 2019 at 18:14
  • +1. Great observation on Daylight changes.
    – Jayant Das
    Commented Mar 5, 2019 at 20:17

You must log in to answer this question.

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