The CreatorCon Call for Content is officially open! Get started here.

getGlideObject() from a glide date/time field

Christian37
Tera Contributor

Hi guys,

I have the following problem:

I´m using the method getGlideObject() to get the value of a glide date/time field in order to check if that time falls within a schedule I have created.

When using the method, It adds 5 hours to actual value of the field?

Why is this? 

Is it because it is taking the time from the object with some default time zone and that´s why it adds 5 hours?

Is there a way to check the time zone in which the date is being stored?

Seems possible, but I can´t find anything related. Please help.

1 ACCEPTED SOLUTION

try below

var schedRec = new GlideRecord('cmn_schedule');
var name = schedRec.get('name', '8-5 weekdays');
var sched = new GlideSchedule(schedRec.sys_id);

var currentDateTime = new GlideDateTime();
currentDateTime.setValue(current.u_tsf_fecha_workshop);

gs.addErrorMessage('currentDateTime after: ' + currentDateTime.getDisplayValue());

if (sched.isInSchedule(currentDateTime)) {

    var dur = new GlideDuration(60 * 60 * 16 * 1000);
    var newDateTime = sched.add(currentDateTime, durToAdd, '');

    // var currentWSDate = current.u_tsf_fecha_wsreal.getGlideObject();

    gs.addErrorMessage('current.u_tsf_fecha_wsreal: ' + newDateTime.getDisplayValue());


} else {
    current.setValue("u_tsf_fecha_workshop", "");
    gs.setRedirect(current);
    current.setAbortAction(true);

}

View solution in original post

9 REPLIES 9

try below

var schedRec = new GlideRecord('cmn_schedule');
var name = schedRec.get('name', '8-5 weekdays');
var sched = new GlideSchedule(schedRec.sys_id);

var currentDateTime = new GlideDateTime();
currentDateTime.setValue(current.u_tsf_fecha_workshop);

gs.addErrorMessage('currentDateTime after: ' + currentDateTime.getDisplayValue());

if (sched.isInSchedule(currentDateTime)) {

    var dur = new GlideDuration(60 * 60 * 16 * 1000);
    var newDateTime = sched.add(currentDateTime, durToAdd, '');

    // var currentWSDate = current.u_tsf_fecha_wsreal.getGlideObject();

    gs.addErrorMessage('current.u_tsf_fecha_wsreal: ' + newDateTime.getDisplayValue());


} else {
    current.setValue("u_tsf_fecha_workshop", "");
    gs.setRedirect(current);
    current.setAbortAction(true);

}

Thank you so much Mike. I have marked the answer as correcy.

So a workaround was to use a glide date time object to get the exact value of the field, it gets the display value and no timezone attribute involved.

I tried using the getDisplayValue method rather than the getGlideObject, but the format was not compatible when it checked if that time fell inside the schedule defined.

 

 

Milind Gharte
Kilo Guru

Hi ,
Here is a link which will help you.

https://docs.servicenow.com/bundle/orlando-application-development/page/script/useful-scripts/refere...

 

If it helps,Please mark Correct and 👍 Helpful.

Warm Regards,

Milind

Hi Milind,

Yeah, I´ve used that article which I found very useful, but the issue is the value that is inside the object that is being read out loud is in another time zone...

asifnoor
Kilo Patron

Hi,

By default new GlideDateTime() returns the time in GMT.

you can fetch the date/time in your timezone using this

var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");

var gdt = new GlideDateTime();

gdt.setTZ(tz);

gdt.setValue(your_value);

gs.print(gdt.getDisplayValue());