Fix addDays() on the DateTime type - Ideas - Salesforce Trailblazer Community

All Ideas

Idea Details

Post an Idea
130  Points
Idea has been posted. Give it an upvote or downvote.

Fix addDays() on the DateTime type

Operational Reporting

I'll use a pop quiz to start the idea:  what should be the debug result of this code?
datetime myDate = datetime.newInstance(2009, 11, 1);
datetime newDate = mydate.addDays(2);

Hint: I'm running this in a US time zone org.  For those of who think it should be 2009-11-03, please read on.

Right now would output 2009-11-02.  I have confirmed this behavior with Tech Support, and they'll report it to R&D.  It's actually fairly easy to see what's going on.  addDays() on DateTime just adds 24 hours per day - blindly.  Unfortunately, some days in a DST (Daylight Saving Time) country are 25 hours.  2009-11-01 is one of the dates.  With the Date/DateTime translation in between, you end up with counter-intuitive results. 

SFDC's advice?  Use Date type only if you only care about the date part.  So for things like LastModifiedDate, convert them to Date first.  That being said, I still think this behavior should be corrected.  Either don't have addDays() on the DateTime type, or make it a little more savvy (time zone aware).  Otherwise it's easy for people to misuse.

Let's finish with another example, shall we? 
datetime myDate = datetime.newInstance(2010, 03, 15);
datetime newDate = mydate.addDays(-1);

I really hope one day it'd be 2010-03-14 for me!

Merge Idea · Flag

  • Upvotes
  • Downvotes



from AppExchange


Help us to keep IdeaExchange clean by pointing out overlapping ideas. We'll investigate your suggestion and merge the ideas if it makes sense.



Thanks for your merge suggestion. We will review it shortly and merge the ideas if applicable.

Salesforce takes abuse situations very seriously. Examples of abuse include but are not limited to posting of offensive language or fraudulent statements. To help us process your request as quickly as possible, please fill out the form below describing the situation. For privacy and security reasons, the final outcome of an abuse case may not be revealed to the person who reported it.


Thank you for your feedback. We take abuse seriously and will investigate this issue and take appropriate action.