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
DJ 367DJ 367 

How to convert system.now to 2019-05-02T14:15:17Z

Hello All,

can someone please help me to convert dataTime to 2019-05-02T14:15:17Z.
I am using below code but output is 2019-05-03T00:00:00Z  , HH:MM:SS are zero. Thanks
dateTime d = system.now();
        System.debug('***-->'+d);
        Datetime myDT = datetime.newInstance(d.year(), d.month(),d.day());
        String myDate = myDT.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
		String formatted = myDateTime.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ');

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

newInstance is used to construct a Datetime from Integer representations in the local time zone.

You can try below code:
dateTime d = system.now();
System.debug('d ***-->'+d);

String myDate = d.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
System.debug('myDate ***-->' + myDate);

String formatted = d.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ');
System.debug('formatted ***-->' + formatted);

I hope it helps you.

Kindly 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. It will help to keep this community clean.

Thanks and Regards,
Khan Anas