Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

venkatiyer1
Giga Guru

Hi all,

Was trying to retrieve hours, minute seconds but didn't find a direct utility that could help me with it. I figured out a solution using the date object and wanted to share with you all below. 

var GetTimeValues = Class.create();
GetTimeValues.prototype = {
	/**
	Usage 
	new GetTimeValues().getSeconds() return seconds of local instance time
	new GetTimeValues().getMinutes() return minutes of local instance time
	new GetTimeValues().getHours() return hrs of local instance time
	Alternatively you can pass a date time string or display value of a date time column
	
	new GetTimeValues("2018-04-16 05:15:13").getHours() will return 5
	*/
	initialize: function(gdt) {
	    try{
			if(JSUtil.nil(gdt)) {
				this.gdt = new GlideDateTime(gs.nowDateTime()); // SNOW system time
			} else {
				this.gdt = new GlideDateTime(gdt);
			}
		} catch(ex) {
			gs.log("Exception caused while setting glideDateTime object");
		}
	},
	// Return hours in number type
	getHours : function() {
		var ms = this.gdt.getNumericValue(); // get milliseconds
		var dt = new Date(ms);
		return dt.getUTCHours();
		
	},
	// Return minutes in number type
	getMinutes : function() {
		var ms = this.gdt.getNumericValue(); // get milliseconds
		var dt = new Date(ms);
		return dt.getUTCMinutes();
	},
	// Return seconds in number type
	getSeconds : function() {
		var ms = this.gdt.getNumericValue(); // get milliseconds
		var dt = new Date(ms);
		return dt.getUTCSeconds();
		
	},
	
	
	type: 'GetTimeValues'
};

 

Version history
Last update:
‎04-16-2018 10:17 AM
Updated by: