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 in SalesForce by surbhi
• 3,810 points
521 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);
answered Mar 15 by CoolCoder
• 4,360 points

Related Questions In SalesForce

0 votes
1 answer

Json response to be deserialized in Apex salesforce lightning

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

answered Mar 2 in SalesForce by surbhi
• 3,810 points
276 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 in SalesForce by surbhi
• 3,810 points
104 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() ='Mar ...READ MORE

answered Mar 17 in SalesForce by CoolCoder
• 4,360 points
50 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 in SalesForce by CoolCoder
• 4,360 points
195 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
1,873 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,010 points
302 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,010 points
182 views
0 votes
1 answer

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

Hi @Vardhan, I took Edureka's Salesforce Training that covers all ...READ MORE

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

edited Dec 22, 2021 by Soumya 216 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 in SalesForce by CoolCoder
• 4,360 points
518 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 in SalesForce by CoolCoder
• 4,360 points
99 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP