^ Click Here

Sunday, June 5, 2011

How to get date and datetime as string in desired format in salesforce.

(I am sure there would be other methods, but one which worked for me( I didn't look further). you need to get a date to use in dynamic query.)

//Create a datetime
Datetime dtDate = Datetime.newinstanceGmt(2011,5,21,0,0,0);

// Change the format according to your need and get the date as a string
string dateCh = dtDate.format('yyyy-MM-dd');

string otherdate = dtDate.format('dd-MM-yyyy');

(this was no big deal but if you need to get a datetime for a query(SOQL) this method would be very useful.)

string cdDateCh = dtDate.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

ex.-

List = [select name, Account.name from Contact where createdDate > :cdDateCh];




No comments:

Post a Comment