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
FilikinFilikin 

datetime format returning wrong year

Hi,
I wrote this code back in 2014, it has been running fine until yesterday when a customer compained that the year was wrong:
     String collectionDat =  datetime.newInstance(Date.newInstance(2020, 12, 28), time.newInstance(0,0,0,0)).format('YYYY-MM-dd');

this should return '2020-12-28'
now it returns '2021-12-28'

any ideas?
Best Answer chosen by Filikin
VinayVinay (Salesforce Developers) 
Hi Filikin,

The capital Y merge field is the WEEK YEAR i.e. 'YYYY' is the calendar year for the WEEK that the day falls in, which is determined by the calendar year for the SATURDAY of that week.

The lower case y merge field is the CALENDAR YEAR i.e. 'yyyy' is the calendar year as of that DAY.

Reference:
https://help.salesforce.com/articleView?id=000352245&language=en_US&mode=1&type=1

Hope this helps...

Thanks,

All Answers

VinayVinay (Salesforce Developers) 
Hi Filikin,

The capital Y merge field is the WEEK YEAR i.e. 'YYYY' is the calendar year for the WEEK that the day falls in, which is determined by the calendar year for the SATURDAY of that week.

The lower case y merge field is the CALENDAR YEAR i.e. 'yyyy' is the calendar year as of that DAY.

Reference:
https://help.salesforce.com/articleView?id=000352245&language=en_US&mode=1&type=1

Hope this helps...

Thanks,
This was selected as the best answer
FilikinFilikin
many thanks Vinay
VinayVinay (Salesforce Developers) 
Cheers... Please mark as Best Answer above information was helpful so that it can help others in the future.

Thanks,