How to add days to date time in Salesforce Apex

0 votes

Hi I am using Salesforce Apex, I have a date as String as below. I need to add days to it using Apex.

String dateTime = '2017-07-08T23:59:59Z';

If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?

Mar 14, 2022 in SalesForce by surbhi
• 3,820 points
7,529 views

1 answer to this question.

0 votes

Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.

To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:

DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);

If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:

DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);

Hope this helps!

Get your Salesforce Certification today to become a certified admin & app builder.

Thanks!

answered Mar 15, 2022 by CoolCoder
• 4,420 points

edited Jun 19, 2023 by Khan Sarfaraz

Related Questions In SalesForce

0 votes
1 answer

Json response to be deserialized in Apex salesforce lightning

Because some fields in Apex Salesforce are ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
3,558 views
0 votes
0 answers

How to Write the Test class for Salesforce Apex Aura Enabled class?

stuck in here to write a test ...READ MORE

Mar 15, 2022 in SalesForce by surbhi
• 3,820 points
2,056 views
0 votes
1 answer

How to find an element in Salesforce applauncher pop up using Selenium Webdriver?

Try if this works:- driver.findElement(By.xpath(".//p[text() ='Marketing']")).click(); Hope this helps! Check ...READ MORE

answered Mar 17, 2022 in SalesForce by CoolCoder
• 4,420 points
983 views
0 votes
1 answer

How can I rename my Apex Class name in Salesforce

If you wanted to do it through ...READ MORE

answered Apr 1, 2022 in SalesForce by CoolCoder
• 4,420 points

edited Jun 19, 2023 by Khan Sarfaraz 4,333 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
3,393 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
1,036 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
753 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 725 views
0 votes
1 answer

How to Update RecordTypeId field in Lightning record form in salesforce?

A critical action that messes everything up ...READ MORE

answered Mar 3, 2022 in SalesForce by CoolCoder
• 4,420 points
3,349 views
0 votes
1 answer

How to use Database.Statful in batch apex

Move your insert to the finish method: global ...READ MORE

answered Mar 17, 2022 in SalesForce by CoolCoder
• 4,420 points
1,970 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP