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
TerminusbotTerminusbot 

Date Format Bug? Giving wrong day of the month

I am getting the current date using system.now and then trying to format my date into this format 'MM/dd/yyyy'.
 
When I log the current date is shows today (2016-08-11) which of course is correct. But, when I format the date is reflects the date as yesrterday (08/10/2016).

I am spinning my wheels. Has anyone run into this? Here is what I am doing.
 
DateTime now       = system.now().date();
String day         = now.format('dd');
String nowFormat   = now.format('MM/dd/yyyy','EST');


System.debug('Right now Date: ' + now);
System.debug('Day of the Month: ' + day);
System.debug('Full Date: ' + nowFormat);

 
Best Answer chosen by Terminusbot
Chris  ByromChris Byrom
The problem is you are adding '.date()' when setting your DateTime. This sets the time to 00:00:00 or midnight basically. Remember this is in GMT. So when you format the date it takes the date you set at midnight, and converts it to your time zone, which is actually the day before, as GMT is 4 hours ahead of EST. So if you remove '.date()' I believe you will get what you want.

All Answers

Chris  ByromChris Byrom
The problem is you are adding '.date()' when setting your DateTime. This sets the time to 00:00:00 or midnight basically. Remember this is in GMT. So when you format the date it takes the date you set at midnight, and converts it to your time zone, which is actually the day before, as GMT is 4 hours ahead of EST. So if you remove '.date()' I believe you will get what you want.
This was selected as the best answer
TerminusbotTerminusbot
Thank you. Was spinning my wheels way too long on that one.