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.

Issue with timezone conversion with EST time zone.

Hemanth Naik
Tera Guru

Hello community

I have a requirement that in change form I have planned start date field when I select date and time in any time zone it should be converted into EST/EDT and display in another field which is planned start date (EST) field for this I have written these scripts

 

Script include:

var ConvertToESTutil = Class.create();
ConvertToESTutil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    convertDates: function() {
        var mydate = new GlideDateTime(this.getParameter('sysparm_date').toString());
        var gd = new GlideDateTime();
        var tz = Packages.java.util.TimeZone.getTimeZone("EST5EDT");
        gd.setTZ(tz);
        gd.setValue(mydate);
        return gd.getDisplayValue();

    },

    type: 'ConvertToESTutil'
});
 
client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue === '') {
      return;
   }
var startDate = g_form.getValue('start_date');
    var ga = new GlideAjax('ConvertToESTutil');
    ga.addParam('sysparm_name', 'convertDates');
    ga.addParam('sysparm_date', startDate);
    ga.getXML(gaCallback);

    function gaCallback(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        g_form.setValue('u_planned_end_date_est', answer);
    }
}
 
Now the issue is when I select date and time for example in GMT it is correctly converting into EST/EDT and displaying in EST see image 1
but when I select date and time in us/eastren which is nothing but EST/EDT, but the converted time is showing 4 hrs. behind, EST/EDT see image 2
Can someone please help me on this.
3 REPLIES 3

Bhavya11
Kilo Patron
Kilo Patron

Hi @Hemanth Naik ,

 

i tried something like below it worked for me

Script include:

 

 

var ConvertToESTutil = Class.create();
ConvertToESTutil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    convertDates: function() {
        var mydate = new GlideDateTime(this.getParameter('sysparm_date').toString());
        var gd = new GlideDateTime();
		var timeZone = gd.getUserTimeZone();
		var timeZoneID = timeZone.getID();
        if (timeZoneID != 'US/Eastern') {
            var tz = Packages.java.util.TimeZone.getTimeZone('US/Eastern');
            gd.setTZ(tz);
            gd.setValue(mydate);
            return gd.getDisplayValue();
        } else {
            return mydate;
        }

    },

    type: 'ConvertToESTutil'
});

 

 

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

 

Thanks,

BK

 

 

Hi @Bhavya11 , thank you for your valuable response, The code mentioned is above working fine as expected for us/eastern time zone,
But can you please check by changing your time zone to IST then it should convert to 9 hrs. behind to show in Est right, but it will convert only 4 hrs. behind

The reason for these conversions I came to know that the calendar in instance will run in instance time zone, so it is expected behavior.

Hi @Hemanth Naik 

 

I had similar requirement and I got away with the following BR. You can modify it as per your use case-

 

  var preferredSchedule = current.u_preferred_schedule.getGlideObject(); //This field stores time in Local timezone which is current.location.time_zone basically.
    var time = new GlideDateTime(preferredSchedule);
    var zoneName = current.location.time_zone; 
    var time2 = preferredSchedule.getDate();
    var systemtimeZoneOffSet = time2.getTZOffset();

    gs.log('Case Preferred Schedule GMT Time: ' + time);
    // Set timezone  
    var tz = Packages.java.util.TimeZone.getTimeZone(zoneName);
    time2.setTZ(tz);

    var localtimeZoneOffSet = time2.getTZOffset();

    time.addSeconds((systemtimeZoneOffSet / 1000) - (localtimeZoneOffSet / 1000));
    // Print date/time in desired timezone  
    gs.log('Case Preferred Schedule Time is: ' + preferredSchedule);

    current.u_preferred_schedule_sys = time; // replace custom_field with name of your date/time custom field. Here you will get time in IST.

 

Please mark my answer helpful and correct.

 

Regards,

Amit