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(),datetimeinquiry.day()+1).format('yyyy-MM-dd'); 
Result:2019-06-11

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

I have tried your code and getting the result in yyyy-mm-dd  format. 
 
DateTime datetimeinquiry = datetime.now();
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) 
https://salesforce.stackexchange.com/questions/290147/date-format-yyyy-mm-dd/290154

Your query is answered above check it.

Example Code Snippet:-

Datetime d1 = System.now();
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


https://salesforce.stackexchange.com/questions/80214/issue-with-datetime-formatmm-dd-yyyy


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.

Thanks.