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

Getting Current DateTime in Scheduled Apex

So I have an Apex class that is scheduled to run every hour.  In my class I have 2 class level variables:

public DateTime NOW =;

public DateTime LAST_HOUR = NOW.addHours(-1);


I setup the scheduled job via execute anonymous on 11/30/2012 4:38 PM.  The class has been running every hour however, my 2 DateTime variables are never getting updated.  NOW is always 11/30/2012 4:38 and LAST_HOUR is 11/30/2012 3:38 PM.  I'm using these 2 variables to filter on CreatedDate.

Anyone know how I can get NOW to be the current time when the job runs?

Hi bryano,


I think you can query on the "AsyncApexJob" sobject it will give you all information about uour running apex jaob like (CompletedDate,CreatedDate etc) using this information you can achive your functionlality, I believe it should work.


Select TotalJobItems, Status, ParentJobId, NumberOfErrors, MethodName, LastProcessedOffset, LastProcessed, JobType, JobItemsProcessed, Id, ExtendedStatus, CreatedDate, CreatedById, CompletedDate, ApexClassId From AsyncApexJob where Id='Runing Apex job id'







i don't think nothing wrong what you using.

 try this 


Public datetime NOW  =;