+ Start a Discussion
s_macs_mac 

format string to date and datetime fields in apex salesforce

I have to generate a dynamic soql query,with where condition having date/datetime field but the value I pass to these values is dynamic,eg.,I may pass string having only date but the field in CRM may be dateTime now I have to format the string to a dateTime value and then pass this value in my dynamic query.

eg.,Account has field Date Of Birth which is a date field,but I am getting the DateTime value for it so now I have to format the string to DateTime and use this,and the other scenario to be considered here is when the field is DateTime and if I am getting only Value asTime I have to format it to DateTime and then use it in my dynamic query.

Thanks in Advance
SandhyaSandhya (Salesforce Developers) 
Hi,
 
Datetime myDateTime = DateTime.parse('10/14/2011 11:46 AM');
String dateInStringString = myDateTime.format('MM/dd/yyyy');
system.debug('##dateInStringString - '+dateInStringString);

Please refer below link for more Date methods.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm


Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Sandhya