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
DavserDavser 

DateTime.Format Issue

Hey,

I'm trying to use the DateTime.format()method to create a SOQL query string. The string needs to be dynamic, so am literally creating a String for my query. Obviously I'd like to be able to say:

 

 

myDateTime.format('yyyy-MM-ddTHH:mm:ssZ');

 

However this gives me an error saying : "Unrecognized format: yyyy-MM-ddTHH:mm:ssZ".

 

Am I missing something really obvious here, how else should I be formatting a datetime for a soql query?

 

Any ideas are greatly appreciated!

David Walshe

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You need to explicitly quote the T and Z, or you'll get an error.

 

Example:

 

 

system.debug(system.now().format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\''));

Edit: Also remember, if you do use a function that returns the current time zone instead of GMT, to pass the datetime through DateTime.valueOfGmt() first.

Message Edited by sfdcfox on 11-10-2009 03:18 AM