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
IntegrationGuyIntegrationGuy 

Issue with Datetime().format() in Batch apex

String myDate = Datetime.now().addDays(14).formatGmt('MM/DD');
System.debug('mydate'+myDate);

Works fine when I execute from Developer console, today is 24 Jan and it prints 02/07 which is correct.

But when I execute in a Batch class it prints 02/38 which is wrong, why ?

Best Answer chosen by IntegrationGuy
IntegrationGuyIntegrationGuy

The issue was formatGmt('MM/DD').
If you use formatGmt('MM/DD'), it will calculate through an year(till 365).

But to add just 14 days use formatGmt('MM/dd') (dd in small).

but still Thank you for the reply.

All Answers

Muzammil BajariaMuzammil Bajaria
In developer console also, I am getting 02/38. Try below code for MM-DD format
 
DateTime myDate = Datetime.now().addDays(14);
system.debug(String.valueOf(myDate).substring(5,10));
Mark the answer as best if this solves your problem.

Thank You,
Muzammil Bajaria
 
IntegrationGuyIntegrationGuy

The issue was formatGmt('MM/DD').
If you use formatGmt('MM/DD'), it will calculate through an year(till 365).

But to add just 14 days use formatGmt('MM/dd') (dd in small).

but still Thank you for the reply.

This was selected as the best answer