Showing posts with label Date Time Calculation in Apex. Show all posts
Showing posts with label Date Time Calculation in Apex. Show all posts

Tuesday, August 19, 2014

Different Date Time Calculation steps in Apex.

Different Date Time Calculation steps in Apex.


"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."







 
| ,