How to make Date/Time field dependent on the Time Zone ?

Veer MS
Kilo Guru

Hello Experts,

I have a custom field on the sys_user table called 'Date TZ', which is a Date/Time type.

I have a requirement where I need to show the time in this field, what ever the user chooses from the 'Time Zone' field.

For Example: if the user chooses 'Europe/London', the 'Date TZ' should show the time in 'Europe/London', as of now it shows in the default system time.

Is there a way to do this ?

find_real_file.png

Thanks

Veer

1 ACCEPTED SOLUTION

Veer MS
Kilo Guru

Hi,

I got this working with a script include and a onchange client script.

Script Include:

var DateTimeZone = Class.create();
DateTimeZone.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    setDateTimeZone: function() {
        var timez = this.getParameter('sysparm_time_zone');
        var tz = Packages.java.util.TimeZone.getTimeZone(timez);
        var gr = new GlideRecord("sys_user");
        gr.addActiveQuery();
        gr.query();
        if (gr.next()) {
            var time = new GlideDateTime();
            time.setTZ(tz);
            var timeZoneOffset = time.getTZOffset();
            time.setNumericValue(time.getNumericValue() + timeZoneOffset);
            return time;
        }

    },

    type: 'DateTimeZone'
});

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var timezone = g_form.getValue('time_zone');
    var ga = new GlideAjax('DateTimeZone');
    ga.addParam('sysparm_name', 'setDateTimeZone');
    ga.addParam('sysparm_time_zone', timezone);
    ga.getXML(getDateTimeZone);

    function getDateTimeZone(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer").toString();
        g_form.setValue('u_date_tz', answer);
    }
    //Type appropriate comment here, and begin script below

}

View solution in original post

7 REPLIES 7

Munender Singh
Mega Sage

Hi,

You can try the following way to convert the local time to the desired timezone time:

 

var tz = Packages.java.util.TimeZone.getTimeZone(current.time_zone_field);

var time = new GlideDateTime();

time.setTZ(tz);

time.setValue(current.date_time_field);  

 

Regards,

Munender

Hi Munender,

Thanks for the quick reply, but it is now setting the hours.

Hours should be 8, but it shows 00.

find_real_file.png

Hi,

 

I have noticed that is the user's time zone is 'GMT', then this script gives the expected results.

But if the user's time zone is other then 'GMT' it does not give the same result.

It should give the same results for all users.

How do I achieve that ?

Hi Veer,

 

I suggest you to convert whatever timezone it maybe, to UTC format and then set it, lets say for ex user timezone is PST, cpnvert to UTC and then set it according to TZ. Try this,

 

Hope this helps.

 

Regards

Omkar Mone