You need to sign in to do that
Don't have an account?
JeriMorris
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!
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
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).
Below is the formula to calculate the total number of minutes from two datetime fields:
Total number of minutes = (LastModifiedDate-CreatedDate)*24*60;
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!
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.
http://www.salesforcefast.com/2011/06/find-minutes-between-two-times.html
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
@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:
see the pattern.
This answers the above question with apex:
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