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
StormConsultingStormConsulting 

Add 1 day to a Datetime object

Is it possible to add a day to a Datetime variable, for example myDate contains 31/08/2008, how can I add one day to it, which will make it become 01/09/2008?

Cheers
mikefmikef
please take at look at Apex docs under Date Methods, or Datetime Methods.
There is a method called addDays().

This method will respect adding a day that might push the date to the next month.
SuperfellSuperfell
DateTime t = DateTime.Now().AddDays(1);
System.Debug(t);

08:29:18 INFO - 20080702152918.067:AnonymousBlock: line 2, column 1: 2008-07-03 15:29:18
Eric_SantiagoEric_Santiago
What about subtracting a day? Is it Today().AddDays(-1) ?
Dumber_Texan2Dumber_Texan2
Correct! Run this in your Developer Console. This will subtract 1 day from the current date and time.
 
DateTime newDate = DateTime.Now().AddDays(-1);
system.debug('Time = ' + newDate);