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
Mahesh Babu 3Mahesh Babu 3 

Subtraction for two datetime fields

Hi All,

I am trying to subtract two datetime fields but i got below error message,
" Date/time arithmetic expressions must use Integer, Decimal or Double arguments "

My code is in below

CronTrigger ct = [SELECT Id,NextFireTime,starttime, CronJobDetail.name FROM CronTrigger where CronJobDetail.name = 'samplejob001' limit 1];
integer temp =  ct.NextFireTime - ct.starttime;
System.debug('=====>'+temp);

Please guide me, how to solve this issue.
 
Best Answer chosen by Mahesh Babu 3
Anupam RastogiAnupam Rastogi
Hi Mahesh,

In that case you can use the following code. This returns the number of hours of difference between the dates.
 
CronTrigger ct = [SELECT Id,NextFireTime,starttime, CronJobDetail.name FROM CronTrigger where CronJobDetail.name = 'samplejob001' limit 1];

Long milliSeconds =  ct.NextFireTime.getTime() - ct.starttime.getTime();
Long hours = milliSeconds/3600000;

System.debug('=====>'+hours);

Thanks
AR

If you find the reply useful that solved your problem then mark it as best answer.


 

All Answers

Anupam RastogiAnupam Rastogi
Hi Mahesh,

Use the following code instead. This will work.
 
CronTrigger ct = [SELECT Id,NextFireTime,starttime, CronJobDetail.name FROM CronTrigger where CronJobDetail.name = 'samplejob001' limit 1];

Integer temp =  ct.starttime.date().daysBetween(ct.NextFireTime.date());

System.debug('=====>'+temp);

Thanks
AR

If you find the reply useful that solved your problem then mark it as best answer.
Mahesh Babu 3Mahesh Babu 3
Thanks for your quick reply Anupam,

I want to get this in hours format not in days, actual results will be lessthan 24 hours
Anupam RastogiAnupam Rastogi
Hi Mahesh,

In that case you can use the following code. This returns the number of hours of difference between the dates.
 
CronTrigger ct = [SELECT Id,NextFireTime,starttime, CronJobDetail.name FROM CronTrigger where CronJobDetail.name = 'samplejob001' limit 1];

Long milliSeconds =  ct.NextFireTime.getTime() - ct.starttime.getTime();
Long hours = milliSeconds/3600000;

System.debug('=====>'+hours);

Thanks
AR

If you find the reply useful that solved your problem then mark it as best answer.


 
This was selected as the best answer