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

Mike Patel
Tera Sage

So all datetime is saved in GMT zone so it will give you that time. Can you share your script so I can help you with the script.

Sure, Mike. This is the Script...

 

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


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

if (sched.isInSchedule(currentDateTime)) {

var timeToAdd = 57600; // adds 16 hrs to define the true due date
var durToAdd = new GlideDuration(timeToAdd * 1000 * 2);
var newDateTime = sched.add(currentDateTime, durToAdd);

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

// gs.addErrorMessage('current.u_tsf_fecha_wsreal.getGlideObject(): ' + currentWSDate);


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

}

try (make sure you are getting correct value in error message)

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.setDisplayValue(current.u_tsf_fecha_workshop);

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

if (sched.isInSchedule(currentDateTime)) {

    var timeToAdd = 57600; // adds 16 hrs to define the true due date
    var durToAdd = new GlideDuration(timeToAdd * 1000 * 2);
    var newDateTime = sched.add(currentDateTime, durToAdd, '');

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

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


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

}

It is adding 8 hours now so I guess it is now in a different time zone than before (5hrs)..

 

Do you think this snippet would work?

 

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

var time = new GlideDateTime();

time.setTZ(tz);

time.setValue(current.sysapproval.end_date);