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
Rakul SrivastavRakul Srivastav 

need the query to fetch records between a fixed time.

How can i get records for a query where i need to get the records which are created between 8p.m PST the previous day to 8p.m PST today. Please help me with the query in Apex.
CharuDuttCharuDutt
Hii 
Try Below Query
Date myDate = date.newinstance(2011, 1, 1); 
Date myDate2 = date.newinstance(2011, 12, 31);
Select Id,CreatedDate from Accounto where CreatedDate >: myDate and CreatedDate <: myDate2
----------------------
SELECT Id,CreatedDate 
FROM Account
WHERE CreatedDate >= 2021-06-21T20:00:00Z AND CreatedDate <= 2021-06-22T20:00:00Z
--------------------------------
SELECT Id FROM Account WHERE CreatedDate = YESTERDAY And CreatedDate = TODAY 
Please Mark It As Best Answer If It Helps
Thank You!

 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

//DateTime dt=DateTime.newInstance(year, month, day, hour, minute, second);
        DateTime myDate = DateTime.newinstance(2021, 6, 11,8,0,0);
        DateTime myDate2 = DateTime.newinstance(2021, 6, 20,8,0,0);
        List<Opportunity> oppLst = [Select CreatedDate, Id, LastModifiedDate from Opportunity where CreatedDate >: myDate2 AND CreatedDate <:  myDate];


Please mark it as the Best Answer so that other people would take reference from it.

Thanks.

Rakul SrivastavRakul Srivastav
Thanks for the reply. My requirement is:
I have a date field as '2021-05-17T22:00:59.000+0000', now i need to check if that field falls in between Yesterday 8p.m PST to today's 8p.m PST. and yesterday and today is not a fixed date, i need to use it in query. The query which you have replied
"SELECT Id,CreatedDate FROM Account WHERE CreatedDate >= 2021-06-21T20:00:00Z AND CreatedDate <= 2021-06-22T20:00:00Z" is for a fixed date, i want here yesterday and today which is dynamic and time in 8p.m PST.
I'm new to Salesforce, please guide me.