Salesforce : Converting DateTime to format YYYY-MM-DDThh:mm:ssZ


I spend lots of my time finding a solution for converting the datetime into the format (YYYY-MM-DDThh:mm:ssZ) so that it can be used in SOQL query.  SOSL query returns the datetime in format(YYYY-MM-DDThh:mm:ssZ) so for comparison one need to convert it into that format.

System.debug(DateTime.now().format(‘yyyy-MM-dd\’T\’hh:mm:ss\’z\”));

I hope this will save your time. 🙂

4 Responses to Salesforce : Converting DateTime to format YYYY-MM-DDThh:mm:ssZ

  1. Ivan says:

    awesome solution

  2. Sneha says:

    Thanks a lot 🙂

  3. shruthi says:

    it doesnt work. throws an error- Invalid identifier ‘‘yyyy’. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 – 9), ‘$’, ‘_

Leave a comment