So you could do something like this:
Decimal MilliSeconds = EndTime.getTime() -StartTime.getTime() ;
Decimal HourConvert = Millisecds / (1000.0*60.0*60.0)
When you use .getTime() it returns the number of Milliseconds since 1970.
Subtract the two to get the total milliseconds between the two dateTimes.
Then you take the milliseconds to calculate up to get your hours. 1000 (Seconds), * 60 (Minutes) * 60 (hours)
Edit Based On actually reading the questions correctly*
So the above gives you total time.
To get the Night/Day hours you need to break it up.
- Figure out Hours of Day and Night for the Start Time
- Figure out Hours of Day and Night for End Time
- Figure out Total WHOLE DAYS between Start and End time. Then you can split that number to get your Day/Night Hours.
So that's the basis of this. Below is a class (Quick and Dirty) to do just what I listed above. What's not included is the Rounding up of the values.
public class TimeCalc {
Public static string TimeCalcHr(DateTime InputStart, Datetime InputEnd){
Datetime DayStart = datetime.newinstance(InputStart.Year(),InputStart.Month(), Inputstart.Day(),6,0,0);
Datetime DayEnd = datetime.newinstance(InputStart.Year(),InputStart.Month(), Inputstart.Day(),12,0,0);
Decimal StartingDaysMS = InputStart.getTime() - DayStart.getTime();
Decimal StartingNightsMS = InputStart.getTime() - DayEnd.getTime();
if(StartingDaysMS <0){
StartingDaysMS =0;
}
if(StartingNightsMS <0){
StartingNIghtsMS = 0;
}
//Repeat for End Date
DayStart = datetime.newinstance(InputEnd.Year(),InputEnd.Month(), InputEnd.Day(),6,0,0);
DayEnd = datetime.newinstance(InputEnd.Year(),InputEnd.Month(), InputEnd.Day(),12,0,0);
Decimal EndingDaysMS = InputEnd.getTime() - DayStart.getTime();
Decimal EndingNightsMS = InputEnd.getTime() - DayEnd.getTime();
if(EndingDaysMS <0){
EndingDaysMS =0;
}
if(EndingNightsMS <0){
EndingNIghtsMS = 0;
}
//Figure out hours between
decimal NightMSInADay = DayEnd.GetTime() - DayStart.getTime();
Decimal DayMSInADay = DayStart.Adddays(1).getTime()-DayEnd.GetTime();
//figure out number of days between times
integer NumOfDays = InputStart.Date().daysBetween(InputEnd.Date());
Decimal NumMSBetweenDays = NumOfDays * DayMSInADay;
Decimal NumMSBetweenNights = NumOfDays * NightMSInADay;
//Add Everything Togeter
Decimal TotalNightMS = NumMSBetweenNights + EndingNIghtsMS + StartingNightsMS;
Decimal TotalDaysMS = NumMSBetweenDays+ EndingDaysMS+ StartingDaysMS;
Decimal TotalNightHrs = TotalNightMs / (1000*60*60);
Decimal TotalDayHrs = TotalDaysMS / (1000*60*60);
String Output = 'Night Hours: ' + string.Valueof(TotalNighthrs) + ' Day Hours: ' + string.valueof(TotalDayHrs);
return Output;
}
}
Can't help you with JS code, but I can with APEX. There's a freebie Stop Watch class that's part of Apex Utilities which you can get from either the App Exchange or get the source code here. If you downloaded the source code, I'd think you could readily incorporate portions of it into a custom controller for your wizard.
Best Answer
No.
No, that is not correct. Because the code was called after halfway through the session time, the timer is reset back to two hours. As long as you're using your session, it does not time out.
That's not correct (as per above). The session may have been created days ago, so long as it was in use the whole time, it's still active.
As it says in the documentation:
Conceptually, you could get the LastModifiedDate and add the NumSecondsValid from the
getCurrentSession()
map from above. Note that the date format isn't a typical format you'd see in Salesforce, so you'll have to "manually" parse it.