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.

Convert From any timezone to UTC

vps
Tera Contributor

How to convert time from any timezone to UTC?

I have a catalog form with the date, time, and timezone field. When any user submits the form, we need to email the time in UTC format

find_real_file.png

7 REPLIES 7

Allen Andreas
Tera Patron

Hi,

By default the system stores the values in UTC format.

With that said, since you're breaking the pieces out, you'd need to create a mail script to get the current variables for the date and time and build a GlideDateTime object, then print the value as that would be UTC format such as (EXAMPLE😞

var d = current.variables.date_field.toString();
var t = current.variables.time_field.toString();
var gdt = new GlideDateTime();
gdt.setValue(d + " " + t);
template.print("The date/time in UTC is: " + gdt.getValue());

Then in your notification body, you'd use:

${mail_script:name_of_mail_script}

Additional resources for you to review:

Mail Script: https://developer.servicenow.com/dev.do#!/learn/learning-plans/quebec/new_to_servicenow/app_store_le...

GlideDateTime: https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_APIRef#r_ScopedGl...

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


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

How to pass the timezone from where I need to convert?

 

Hi,

The timezone is irrelevant because you're converting to UTC anyway?

No matter what a user puts in to a date/time field, the back-end value is the UTC time.

Please refer to what I've mentioned above.

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


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hitoshi Ozawa
Giga Sage
Giga Sage

I've created a timezone table with columns "timezone" and "offset". Entered timezone and offset in seconds.

Filled the table from csv file from following github project.

https://github.com/bproctor/timezones/blob/master/timezones.csv

e.g.

timezone offset
US/Central -21600
Asia/Tokyo 32400

Script:

    var tZone = current.variables.timezone.toString();
    var offset = getOffset(tZone);

    var tDate = current.variables.termination_date.toString();
    var tTime = current.variables.termination_time.toString();
    tDate += " " + tTime + ":00";
    var gdt = new GlideDateTime(tDate);
    tDate = gdt.getNumericValue();
    tDate = tDate - (offset * 1000);
    var date = new Date(tDate);
    var d = zeroPad(date.getDate());
    var m = zeroPad(date.getMonth() + 1);
    var y = date.getFullYear();
    var hr = zeroPad(date.getHours());
    var min = zeroPad(date.getMinutes());
    var sec = zeroPad(date.getSeconds());
    var dateFormat = gs.getSession().getUser().getDateFormat();
    dateFormat = dateFormat.replace('dd', d);
    dateFormat = dateFormat.replace('yyyy', y);
    dateFormat = dateFormat.replace('MM', m);
    dateFormat = dateFormat.replace('HH', hr);
    dateFormat = dateFormat.replace('mm', min);
    dateFormat = dateFormat.replace('ss', sec);

    template.print(dateFormat);

    function zeroPad(val) {
        return ('0' + val).slice(-2);
    }

    function getOffset(timezone) {
        var gr = new GlideRecord('u_timezone');
        if (gr.get('u_timezone', timezone)) {
            return gr.u_offset;
        }
        return 0;
    }