0 votes

I have my locale set at (GMT-05:00) Central Daylight Time (America/Chicago). And I am using the below Apex.

DateTime d2 = System.Today() ;
system.debug('date is '+d2); // date is 2021-04-10 00:00:00
String dateStr1 =  d2.format('MM/dd/yyyy') ;
system.debug('formatted date is '+datestr1); // formatted date is 04/09/2021

Why is the date changing before and after formatting?

1 Answer

0 votes
Best answer

This happens because System.debug outputs the original DateTime value as GMT, but when you use DateTime.format(formatStr), it is rendered in the user's time zone. If the user is West of GMT-00:00, the day will still be the previous day compared to GMT, so the day is lost. Use DateTime.formatGMT(formatStr) if you want the same output as System.debug.

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
You can donate any amount for Orphans village using following QR Code.