Round-up to the nearest 15mins using in-built methods

SandaminiM
Tera Contributor

Hi Community 😊

 

 

var CutOffTimeGeneration = Class.create();
CutOffTimeGeneration.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	calculateCutOffTime: function() {
		var gtime1 = new GlideDateTime();
		gs.info("gtime1 : " + gtime1);
		var cutOffTime;
		var timeGapString;
		var timeGap;
		var priority;

		var sysId = this.getParameter('sysparm_sysId');
		gs.info("sysId from the ticket : "+sysId);

		var caseGR = new GlideRecord('sn_csm_case');

		if (caseGR.get(sysId)) { 
				priority = caseGR.getValue('priority');
				gs.info("Priority : "+priority);
		}

		var mapping = gs.getProperty('sn_customerservice.priorityToTimeMapping');
		var priorityToTimeMap = JSON.parse(mapping);

		timeGapString = priorityToTimeMap[priority];
		timeGap = parseInt(timeGapString, 10);
		gs.info("TimeGap : "+timeGap);

		// Adding the relevant time gap to the current time
		gtime1.addSeconds(timeGap * 60);		

		var currentTime = gtime1.getDisplayValue();
		gs.info("cutofftime before rounding off : " + currentTime);
		var dateParts = currentTime.split(" ");
		
		var timeParts = dateParts[1].split(":");
		gs.info("timeParts[1] : " + timeParts[1]);

		var minutes = parseInt(timeParts[1]);		

		gs.info("minutes : " + minutes);

		var flag = false;

		// Round-up to the nearest 15 minutes
		var remainder = minutes % 15;
		gs.info("remainder : " + remainder);
		if (remainder !== 0) {
			minutes += (15 - remainder);

			// Handle overflow 
			if (minutes >= 60) {
				flag = true;
				gtime1.addSeconds(60*60);
			}
		}

		currentTime = gtime1.getDisplayValue();
		dateParts = currentTime.split(" ");
		timeParts = dateParts[1].split(":");

		// Format the time for displaying purposes
		var roundedTime = timeParts[0] + ':' + (flag ? '00' : minutes) + ':00';
		var roundedDateTime = dateParts[0] + ' ' + roundedTime;

		gs.info(roundedDateTime);
		return roundedDateTime;	
	},

    type: 'CutOffTimeGeneration'
});

 

 

The above Script Include contains my implementation, but the entire calculation slows down the process. I want to know if there are any built-in methods or more efficient ways to achieve this.

 

Thanks in advance 💕

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @SandaminiM 

Please try the below script:

var gdt = new GlideDateTime(); // Current Date and Time
var milliseconds = gdt.getNumericValue(); // Get time in milliseconds
var fifteenMinutes = 15 * 60 * 1000;

// Use Math.ceil, Math.floor, or Math.round based on your requirement
// Uncomment the desired rounding logic:

// Round UP to the nearest 15 minutes
var roundedMilliseconds = Math.ceil(milliseconds / fifteenMinutes) * fifteenMinutes;

// Round DOWN to the nearest 15 minutes
// var roundedMilliseconds = Math.floor(milliseconds / fifteenMinutes) * fifteenMinutes;

// Round to the NEAREST 15 minutes
// var roundedMilliseconds = Math.round(milliseconds / fifteenMinutes) * fifteenMinutes;

// Set the rounded value back to a GlideDateTime object
gdt.setNumericValue(roundedMilliseconds);

// Print the result
gs.print("Rounded time: " + gdt.getDisplayValue());

"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

Thank You!
Juhi Poddar

View solution in original post

1 REPLY 1

Juhi Poddar
Kilo Patron

Hello @SandaminiM 

Please try the below script:

var gdt = new GlideDateTime(); // Current Date and Time
var milliseconds = gdt.getNumericValue(); // Get time in milliseconds
var fifteenMinutes = 15 * 60 * 1000;

// Use Math.ceil, Math.floor, or Math.round based on your requirement
// Uncomment the desired rounding logic:

// Round UP to the nearest 15 minutes
var roundedMilliseconds = Math.ceil(milliseconds / fifteenMinutes) * fifteenMinutes;

// Round DOWN to the nearest 15 minutes
// var roundedMilliseconds = Math.floor(milliseconds / fifteenMinutes) * fifteenMinutes;

// Round to the NEAREST 15 minutes
// var roundedMilliseconds = Math.round(milliseconds / fifteenMinutes) * fifteenMinutes;

// Set the rounded value back to a GlideDateTime object
gdt.setNumericValue(roundedMilliseconds);

// Print the result
gs.print("Rounded time: " + gdt.getDisplayValue());

"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

Thank You!
Juhi Poddar