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.

How to format the date time field to UTC time like ""yyyy-MM-dd'T'HH:mm:ss.SSSZ"?

Yuya4
Kilo Contributor

HI all !    I am workking with write the code to format the date time field to UTC Time like ""yyyy-MM-dd'T'HH:mm:ss.SSSZ" in Script include.   But I don't know how for such a long time .    If you have a good idea , please let me know .   

1 件の受理された解決策

Hitoshi Ozawa
Giga Sage
Giga Sage

Try:

var dt = new GlideDateTime();
gs.info(dt);
var iso = new Date(dt.getNumericValue());
gs.info(iso.toISOString());

元の投稿で解決策を見る

2件の返信2

Hitoshi Ozawa
Giga Sage
Giga Sage

Try:

var dt = new GlideDateTime();
gs.info(dt);
var iso = new Date(dt.getNumericValue());
gs.info(iso.toISOString());

Hitoshi Ozawa
Giga Sage
Giga Sage

An example to convert DateTime field to ISO date/time.

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '' || oldValue == newValue) {
        return;
    }

    var ajax = new GlideAjax('DateTimeUtil3');
    ajax.addParam('sysparm_name', 'getISOTime');
    ajax.addParam('sysparm_datetime', newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.showFieldMsg("datetimefield", answer);
        }
    });
}

Script Include:

var DateTimeUtil3 = Class.create();
DateTimeUtil3.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getISOTime: function() {
        var dt = this.getParameter('sysparm_datetime');
        var gdt = new GlideDateTime(dt);
        var isoDt = new Date(gdt.getNumericValue());
        return isoDt.toISOString();
    },
    type: 'DateTimeUtil3'
});

Result:

find_real_file.png