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
smriti sharan19smriti sharan19 

Issue while converting date in yyyy-mm-dd Format

I am trying to convert date in yyyy-mm-dd format but I am getting result in yyyy-dd-mm format.

Initial Value of DateField: 
datetimeinquiry=2019-06-11 00:00:00

Tried newInstance
    convertToStr = DateTime.newInstance(datetimeinquiry.year(),datetimeinquiry.month(),'yyyy-MM-dd'); 

Tried formatGMT:
    system.debug('formatGMT: '+datetimeinquiry.formatGMT('yyyy-MM-dd'));
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Smriti, 

I have tried your code and getting the result in yyyy-mm-dd  format. 
DateTime datetimeinquiry =;
System.debug('datetimeinquiry NOW() '+datetimeinquiry); //2020-11-01 04:19:06
system.debug('formatGMT: '+datetimeinquiry.formatGMT('yyyy-MM-dd'));//2020-11-01
AbhishekAbhishek (Salesforce Developers)

Your query is answered above check it.

Example Code Snippet:-

Datetime d1 =;
String sFormat = d1.format('yyyy/MM/dd HH:mm:ss');
String sFormatGMT = d1.formatGMT('yyyy/MM/dd HH:mm:ss');
System.debug(d1); // DEBUG|2020-01-06 11:13:21 because it always shows the datetime in UTC
System.debug(sFormat); // DEBUG|2020/01/06 12:13:21 because I'm at GMT+1
System.debug(sFormatGMT); // DEBUG|2020/01/06 11:13:21 because I forced the GMT time zone

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.