show calendar date/time fields in EST

Shivam Saxena1
Mega Expert

Hello,

 

We have 2 fields in a form.

1. Expected Start Date/Time

2. Expected End Date/Time

 

The above 2 fields show the system date/time.

 

I want 2 more fields below below the above mentioned fields with name :

1. Expected Start Date/Time (EST)

2. Expected End Date/Time (EST)

 

Requirement :

Expected Start Date/Time (EST) will be copied from Expected Start Date/Time once the user fill this field and show EST time.

Expected End Date/Time (EST) will be copied from Expected End Date/Time once the user fill this field and show EST time.

 

Can someone please help in how to achieve this functionality?

 

 

NOTE : The fields are in scoped application.

1 ACCEPTED SOLUTION

Okay.

Hi create a SI in global scope and then call that Si from your client script. Then you can use the following code to convert your date/time to EST format.

 

Script Include

getConvertedDate: function() {
       var mydate = new GlideDateTime(this.getParameter('sysparm_date').toString());
       var gdt1 = new GlideDateTime();
       //sets to EST
       var tz = Packages.java.util.TimeZone.getTimeZone("EST");
       gdt1.setTZ(tz);
       gdt1.setValue(mydate); 
       return gdt1.getDisplayValue(); //returns the date/time in EST timezone as a string.
},


Mark the comment as a correct answer and also helpful if this helps to solve the problem.

View solution in original post

30 REPLIES 30

Ct111
Giga Sage

You would have to use set timezone like shown in the below blog for your EST fields...

get the vlaue from first two fields and pass it on to it , so that it would take that time and convert to EST

https://snprotips.com/blog/2017/9/12/handling-timezones-in-servicenow-timezoneutil

sekhar kurumoju
Mega Guru

create a Script Include In your Scope :

var TimeZoneUtils = Class.create();
TimeZoneUtils.prototype = {

    
    initialize: function(gdt) {
        this.gdt = (typeof gdt == 'undefined') ? (new GlideDateTime()) : gdt;
    },

    
    getGDT: function() {
        return this.gdt;
    },

    
    getOffsetHours: function() {
        return ((Number(this.gdt.getTZOffset() / 1000) / 60) / 60);
    },

    setTimeZone: function(tz) {
        //ensure we've got a string and that it's lower-case.
        tz = (typeof tz === 'string') ? tz : tz.toString();
        //Validate the TZ string, and get a TimeZone Java object from it.
        tz = this._getTimeZoneFromString(tz);
        this.gdt.setTZ(tz);
        return this.gdt;
    },

    
    getDisplayValue: function() {
        return this.gdt.getDisplayValue();
    },
    getValue: function() {
        return this.gdt.getValue();
    },

    _getTimeZoneFromString: function(tz) {
        //If it's a valid time-zone coming in, bob's our uncle.
        if (this._isValidTimeZone(tz)) {
            if (this.timeZones.hasOwnProperty(tz.toLowerCase())) {
                return this.timeZones[tz.toLowerCase()];
            } else {
                return Packages.java.util.TimeZone.getTimeZone(tz);
            }
        }
        //Otherwise, check if it matches one of our timeZone object properties.
        var shortTZ = this._getShortTimeZoneName(tz);
        if (this._isValidTimeZone(shortTZ)) {
            return this.timeZones[shortTZ.toLowerCase()];
        }

        //If nothing else has returned by now, it means the time zone isn't valid.
        gs.warn('Invalid time zone specified. Time zone: ' + tz, 'TimeZoneUtils Script Include, _getTimeZoneFromString method');
        return false;
    },

    
    _isValidTimeZone: function(tz) {
        var tzObj = Packages.java.util.TimeZone.getTimeZone(tz);
        //If the tz string wasn't valid, then getID will return the string "GMT",
        //which - unless the user specified GMT as the time-zone, will not match the string argument.
        //However, if it does match, OR if the arg is found in the timeZones object, then we're good to go.
        return ((String(tzObj.getID()) === tz) || this.timeZones.hasOwnProperty(tz.toLowerCase()));
    },

    
    _getShortTimeZoneName: function(tz) {
        //Check if the string contains a forward-slash, back-slash, or underscore.
        if (tz.indexOf('\\') >= 0 || tz.indexOf('/') >= 0 || tz.indexOf(' ') >= 0) {
            
            tz = tz.slice(tz.indexOf('\\') + 1).slice(tz.indexOf('/') + 1).trim().replace(/ /g, '_');
        }
        return tz.toLowerCase();
    },

    
    setTZ: function(tz) {
        return this.setTimeZone(tz);
    },

    
    timeZones: {
        alaska:      Packages.java.util.TimeZone.getTimeZone('US/Alaska'),
        eastern:     Packages.java.util.TimeZone.getTimeZone('US/Eastern'),
        central:     Packages.java.util.TimeZone.getTimeZone('US/Central'),
        mountain:    Packages.java.util.TimeZone.getTimeZone('US/Mountain'),
        hawaii:      Packages.java.util.TimeZone.getTimeZone('US/Hawaii'),
        pacific:     Packages.java.util.TimeZone.getTimeZone('US/Pacific'),
        arizona:     Packages.java.util.TimeZone.getTimeZone('US/Arizona'),
        guam:        Packages.java.util.TimeZone.getTimeZone('Pacific/Guam'),
        puerto_rico: Packages.java.util.TimeZone.getTimeZone('America/Puerto_Rico'),
        india:       Packages.java.util.TimeZone.getTimeZone('Asia/Kolkata'),
        utc:         Packages.java.util.TimeZone.getTimeZone('UTC')
    },

    type: 'TimeZoneUtils'
};

// excute on BackGround Script 


var gdt = new GlideDateTime();
gs.print(gdt.getDisplayValue());
var tzu = new TimeZoneUtils(gdt); 
tzu.setTimeZone('US/Eastern'); // pass EST Time Zone 
gs.print(gdt.getDisplayValue());

 

 

//GiledAjax calling
Script Inculde Cilent callable 
var ClientTimeZoneUtils = Class.create();
ClientTimeZoneUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCurrentTimeInTimeZone: function() {
        var tz = this.getParameter('sysparm_tz');
        var tzu = new TimeZoneUtils();
        var gdt = tzu.setTimeZone(tz);
        
        return gdt.getDisplayValue();
    },

    type: 'ClientTimeZoneUtils'
});



Client Side:

var gaTZ = new GlideAjax('ClientTimeZoneUtils'); //initialize GA
gaTZ.addParam('sysparm_name', 'getCurrentTimeInTimeZone'); //specify the function to run
gaTZ.addParam('sysparm_tz', 'guam'); //specify the time-zone as one of the predefined shorthand values
gaTZ.getXML(gaCallback);
function gaCallback(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    g_form.setVlue('The current time in the time-zone you specified, is ' + answer);
}

Please mark reply as Helpful/Correct, if applicable. Thanks!

Hi Sekhar,

 

Thanks for your reply and apologies for late reply.

 

I am getting below error while executing the script in background.

 

find_real_file.png

 

Same error I am getting while executing in form.

find_real_file.png

My form :

 

find_real_file.png

 

Can you please help here?

 

Thanks.

Hi,

package calls are not allowed in scoped application

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader