Loading

Date format 'YYYY' is not the same as 'yyyy' with format function

Publiceringsdatum: Oct 13, 2022
Beskrivning

​Date formats ‘YYYY’ and ‘yyyy’ are NOT the same when applied under format() function of Datetime Class.
Lösning


To see the difference between a simple date format ‘YYYY’ (Week Year) and ‘yyyy’ (Week of Year), run the below snippet of code: 
 

Datetime myDateTime = Datetime.newInstance(2014, 12, 28); system.debug('datetime::'+myDateTime);
system.debug('datetime::'+myDateTime.format('MM/dd/YYYY h:mm a'));
system.debug('datetime::'+myDateTime.format('MM/dd/yyyy h:mm a'));


You will notice that

  • With the simple date format (myDateTime.format('MM/dd/YYYY h:mm a')), the year increases from 2014 to 2015. This happens only from days 28 to 31 for December 2014. In summary, YYYY notation returns the 'week year'.
  • However, when you apply the format (myDateTime.format('MM/dd/yyyy h:mm a')), the year will not change.
Knowledge-artikelnummer

000386309

 
Laddar
Salesforce Help | Article