How to convert the time into minutes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2024 09:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2024 12:18 AM
@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 :
Please mark this as helpful and accept it as a solution if this resolves your query.
Thanks,
Sujatha V.M.
Sujatha V.M.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2024 01:44 AM
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....