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
Vinothini RajanVinothini Rajan 

datetime.formatGMT() for today's date returns next year

Hello All,

Below datetime formatting suddenly not working properly. Below lines returns next year 2022.

DateTime lstUptDate = DateTime.newInstanceGmt(Date.today(), Time.newInstance(0, 0, 0, 0));
System.debug(lstUptDate.formatGMT('YYYY-MM-dd\'T00:00:00\''));


20:12:14.1 (2360027)|USER_DEBUG|[2]|DEBUG|2022-12-29T00:00:00
Best Answer chosen by Vinothini Rajan
VIJAYA DONEPUDIVIJAYA DONEPUDI
Hi
change YYYY to yyyy.
it worked.
User-added image

All Answers

VIJAYA DONEPUDIVIJAYA DONEPUDI
Hi Vinothini Ranjan,
Please try this code

DateTime lstUptDate = DateTime.newInstanceGmt(Date.today(), Time.newInstance(0, 0, 0, 0));
system.debug('formatted date=='+lstUptDate.formatLong());

User-added image
Please refer to the link below:
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_datetime.htm#apex_System_Datetime_formatGmt

 
VIJAYA DONEPUDIVIJAYA DONEPUDI
Hi Vinothini Ranjan,
Also please try 
DateTime lstUptDate = DateTime.newInstanceGmt(Date.today(), Time.newInstance(0, 0, 0, 0));
System.debug(lstUptDate.formatGMT('EEE, MMM d yyyy HH:mm:ss'));
System.debug(lstUptDate.formatGMT('MMM d yyyy HH:mm:ss'));
System.debug(lstUptDate.formatGMT('MM dd yyyy HH:mm:ss'));
System.debug(lstUptDate.formatGMT('MM-dd-yyyy HH:mm:ss'));
all these are working.

Thanks
Vijaya Donepudi.
 
Vinothini RajanVinothini Rajan
Hi Vijaya,

Thanks for your reply. I need it the format YYYY-MM-dd.
It was working before from 25th dec it seems to be picking up with year that's 2022.
VIJAYA DONEPUDIVIJAYA DONEPUDI
Hi
change YYYY to yyyy.
it worked.
User-added image
This was selected as the best answer
Vinothini RajanVinothini Rajan
Thank you Vijaya. It works and just now found the documentation about it.

YYYY' is the calendar year for the WEEK that the day falls in
 'yyyy' is the calendar year as of that DAY.