How to convert the time into minutes

nikhitha24
Tera Guru

Hi Team,

 

Can someone please help me on the script.

Requirement: I have created one record producer. I have used multi row variable set with the variables as start time, end time and meal break time with the type date/time variable. now i need a total hours need to displayed based on the start and end need to convert into minutes and need minus the meal hours and need to give the total hours.

Based on this calculation i need a script.

2 REPLIES 2

Sujatha V M
Kilo Patron
Kilo Patron

@nikhitha24 Please find the code reference snippet as below for your requirement. Hope it helps you for further modification. 

 

var startTime = "2024-04-12 09:30:00";  //Call the variable for dynamic function
var endTime = "2024-04-12 18:37:00";  //Call the variable for dynamic function
var mealHours = new GlideDuration(1.5 * 60 * 60 * 1000); // 1.5 hours of meal time converted to milliseconds and call the variable for dynamic function. 

        var start = new GlideDateTime(startTime);
        var end = new GlideDateTime(endTime);
        
        var duration = new GlideDuration();
        duration = GlideDateTime.subtract(start, end);
        gs.log("Duration of Start and End Time :" + duration.getDisplayValue());

        var total_hours = duration.subtract(mealHours);
        gs.log("Total Hours excluding meal time :" + total_hours.getDisplayValue())

 

 Result : 

SujathaVM_0-1713078758804.png


Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Sujatha V M ,

Here is the function :

 

function calculateTotalHours() {
  
    var startTime = g_form.getValue('start_time');
    var endTime = g_form.getValue('end_time');
    var mealBreakTime = g_form.getValue('meal_break_time');

    if (startTime && endTime && mealBreakTime) {

        var startDate = new Date(startTime);
        var endDate = new Date(endTime);
        var mealBreak = new Date(mealBreakTime);
        var durationInMillis = endDate - startDate;
        var durationInMinutes = durationInMillis / (1000 * 60);
        var mealBreakInMinutes = mealBreak.getHours() * 60 + mealBreak.getMinutes();
        var totalMinutes = durationInMinutes - mealBreakInMinutes;
        var totalHours = Math.floor(totalMinutes / 60);
        var remainingMinutes = totalMinutes % 60;
        g_form.setValue('total_hours', totalHours + ' hours ' + remainingMinutes + ' minutes');

    }
}


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect