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

Passing datetime to query in apex class dynamically.

Select Id from objectname__c where source__c = SF and lastmodifieddate <  2010-02-03T00:15:00.000Z


This query is working fine in eclipse, but I want the same query passing datetime to query in apex class dynamically.



Please let me know how to frame my query in apex class.


Thanks in Advance..




I've been looking for some functions that works for SOQL regarding Dates, but i couldn't find anything, so 

I had to work with Dates from Apex and pass it to my SOQL
public List<Account> getAccountsByDate(Date dateFrom, Date dateTo)
     return [Select Id, Name
                From Account
                Where CreatedDate >= :dateFrom
                          And CreatedDate <= :dateTo];
Message Edited by magdielhf on 02-08-2010 12:13 PM

Thanks for your reply,


What is the format for dateFrom and dateTo in the  parameters you mentioned.can you please explain me how you are doing.




I think that depends on your needs and use case, if you need a DateTime or a Date only, the requirements for those dates ranges, so far I'm using these methods and formats for conversion using Apex, 







Here you might find some suitable information of how to parse Dates,






Let me research on this, will update you after fixing my issue.



I found the answer on this web site: Link to Answer


Code can look like this...

datetime dtStart;
datetime dtEnd;


dtStart =;
dtEnd =
extraWhereForQuery = ' AND CreatedDate >= ' + dtStart.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'') +
             ' AND CreatedDate <= ' + dtEnd.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');