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
JeriMorrisJeriMorris 

Difference between 2 datetime fields

This should be simple, but I'm having a hard time with it. I want to create a formula field that gives me the number of minutes between the record's CreateDate and LastModifiedDate. What does that formula need to look like?

 

Thanks for your help!

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

Below is the formula to calculate the total number of minutes from two datetime fields:

 

Total number of minutes = (LastModifiedDate-CreatedDate)*24*60;

All Answers

sham_1sham_1

I believe the difference between 2 datetime fields returns the number of days between them

 

so the formula could be (LastModifiedDate - CreatedDate) * 24 (Number of hours in a day) * 60 (Number of minutes in an hour).

Pradeep_NavatarPradeep_Navatar

Below is the formula to calculate the total number of minutes from two datetime fields:

 

Total number of minutes = (LastModifiedDate-CreatedDate)*24*60;

This was selected as the best answer
JeriMorrisJeriMorris

Thanks. I actually tried that, but the results didn't look right to me. Trying it again, it definitely does look right -- I must have messed up something when I tried it earlier. Thanks so much for your reply!

suresh.18487suresh.18487

Hi,

Best way to find the difference b/w two date time fields 
//************************************************
Datetime startDate = system.now();// Start date
Datetime endDate = system.now().addHours(60);//End Date
                
integer intDays =  startDate.Date().daysBetween(endDate.Date());
datetime sameDayEndDate = startDate.addDays(intDays);
                
decimal decHours = ((endDate.getTime())/1000/60/60) - ((sameDayEndDate.getTime())/1000/60/60);
//************************************************
intDays : this value show the number of day b/w the given datetime (2 days for the given dates)
decHours : this value shows the number of (12 hours for the given dates )

so total diff is : 2 days and 12 hours
************************************************
If you want to find the diff in days and minutes than modify the foumula accordingly
for minutes :
decimal decMinutes = ((endDate.getTime())/1000/60) - ((sameDayEndDate.getTime())/1000/60);

So total diff is : 2 days and 720 minutes for the given dates .

so you can find the difference in whatever the format you want.

Ramesh KosalairamanRamesh Kosalairaman
check this too ,it give you more idea
http://www.salesforcefast.com/2011/06/find-minutes-between-two-times.html
Mohan Raj 33Mohan Raj 33
@suresh.18487, Hi, I am also facing the problem of calculating to the two date time field difference as like with the following formula 
ABS((Calculating_Date__c - NOW())*24)

this two field are date/time fields (NOW(), Calculating_Date__c) and the result is in number field here it's provide the wrong difference in time so I see the above example and I have the doubt if can I used to your answer  in  formula field to calculatnig the date difference. reply the answer it's really I want .Thank you, Mohan
NZArchitectNZArchitect

@suresh.18487,

Sorry but your attempt is inaccurate for 12 hours of any given day.
I.E. it works fine in the morning 00:00:00 - 11:59:59, but from 12:00:00 if fails.

You could say that added together, your result is the same, but your answer will break someones code in the afternoon.

Everyone, be aware of code that converts datetime to date, you lose your time. But sometime there is no option.

In my example below I convert to millisecs, and work my way back up.

Here is a lengthy way of demonstrating Days:Hours:Minutes:Seconds:

datetime start = datetime.NOW();
datetime later = datetime.now().addHours(60);
decimal millisecs = decimal.valueOf(later.getTime() - start.getTime());
system.debug('millisecs: ' +millisecs);

decimal dDays = millisecs/1000/60/60/24;
system.debug('dDays: ' +dDays);
integer iDays = integer.valueOF(math.floor(dDays));
decimal remainderDays = dDays- iDays;

decimal dHours = remainderDays * 24;
system.debug('dHours: ' +dHours);
integer iHours = integer.valueOf(math.floor(dHours));
decimal remainderHours = dHours - iHours;

decimal dMinutes = remainderHours * 60;
system.debug('dMinutes: ' +dMinutes);
integer iMinutes = integer.valueOf(math.floor(dMinutes));
decimal remainderMinutes = dMinutes - iMinutes;

decimal dSeconds = remainderMinutes * 60;
system.debug('dSeconds: ' +dSeconds);
integer iSeconds = integer.valueOf(math.floor(dSeconds));
decimal remainderSeconds = dSeconds - iSeconds;

system.debug('Days: ' + iDays+' - '+'Hours: ' + iHours+' - '+'Minutes: ' + iMinutes+' - '+'Seconds: ' + iSeconds);

see the pattern.

This answers the above question with apex:

datetime start = datetime.now();
datetime later = start.addHours(60);
decimal minutes = (decimal.valueOf(later.getTime() - start.getTime()))/1000/60;
P5P5
Here is the great code snippet:
 
Long dt1Long = DateTime.now().addDays(-1).getTime();
Long dt2Long = DateTime.now().getTime();
Long milliseconds = dt2Long - dt1Long;
Long seconds = milliseconds / 1000;
Long minutes = seconds / 60;
Long hours = minutes / 60;
Long days = hours / 24;

 
Ganesh MateGanesh Mate
hello JeriMorris,

Try this :-
                Date oldDate = '2018-05-31 00:00:00';
                Date newDate = '2018-06-31 00:00:00';
                Integer numberDaysDue = oldDate.daysBetween(newDate);

Regrads,
Ganesh
Ankit Gupta 340Ankit Gupta 340
Thank you so much @suresh.18487 . It works correctly in my case, where I needed to calculate the no of Hours b/w two given Dates.