logo

NJP

Retrieving hours, min and seconds from the GlideDateTime object or using Instance time

Import · Apr 16, 2018 · article

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'
};
View original source

https://www.servicenow.com/community/developer-articles/retrieving-hours-min-and-seconds-from-the-glidedatetime-object/ta-p/2329818