+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

Datetime.format unexpected behavior

I tried the following today in apex anonymous window and I see an unexpected behavior 
System.debug(Datetime.now());
System.debug(Datetime.now().format('YYYY-MM-DD'));
Why id the DD 84 when I use System.debug(Datetime.now().format('YYYY-MM-DD'))? 
13:09:14.1 (2508337)|USER_DEBUG|[1]|DEBUG|2020-03-24 19:09:14
13:09:14.1 (2587013)|USER_DEBUG|[2]|DEBUG|2020-03-84
Best Answer chosen by Yogesh Biyani
AbhishekAbhishek (Salesforce Developers) 
Hi Yogesh,

I think your query is answered in this blog,

https://salesforce.stackexchange.com/questions/103436/datetime-format-unexpected-behavior

Check it once.

Thanks.

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Yogesh, 

I tried the format listed in the following document to display only date value https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm and It is showing correct value for me

date myDate = date.newInstance(2020, 3, 24);
String dayString = myDate.format();
System.debug('format' + dayString);

You can raise a case with support if you see any odd behavior. Thanks

Anudeep
 
AbhishekAbhishek (Salesforce Developers) 
Hi Yogesh,

I think your query is answered in this blog,

https://salesforce.stackexchange.com/questions/103436/datetime-format-unexpected-behavior

Check it once.

Thanks.
This was selected as the best answer
Yogesh BiyaniYogesh Biyani
Hello Anudeep,

Thanks for looking into this. However, I am using the DateTime class.

Also, I see that System.debug(Datetime.now().format('YYYY-MM-dd')); gives the desired result. Notice I am using dd vs DD in the format string. 

Yogesh

 
Yogesh BiyaniYogesh Biyani
@Abhishek, thanks for the link. I changed the code to use yyyy-MM-dd. 
PRAKASH kumar 116PRAKASH kumar 116
solve: 

date myDate = date.newInstance(2020, 3, 24);
String dayString = myDate.format();
System.debug('format' + dayString);

And Also Follow This 
https://salesforce.stackexchange.com/questions/103436/datetime-format-unexpected-behavior

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Download Movies From Filmyzilla (https://filmyzillatv.com)

Download Movies IN HD 

(https://filmyzillatv.com)