gs.minutesAgo with TZ consideration

tony_fugere
Mega Guru

gs.minutesAgo(-10) returns time in UTC format when used on a field in a template. It would be nice if there were some TZ consideration in a few new [period]Ago methods. Try these out in a date field on a template and apply the template:

javascript:gs.nowDateTime()
javascript:gs.minutesAgo(-10)

Notice the minutesAgo is in UTC while nowDateTime is local time.

6 REPLIES 6

john_roberts
Mega Guru

Try this



//return time 10 minutes ago in user's timezone
var myTime = new GlideDateTime(gs.minutesAgo(-10)).getDisplayValue();


EDIT: I just realized that I "could" technically try to find a way to do this with a nifty GlideAjax object and a Client Script, but the template is already forcing the user into a round-trip server call, so I'm trying to minimize those...

Here's what I'm hoping for that this does not support. A template like this:

find_real_file.png

This shows up in the logs:

org.mozilla.javascript.EcmaError: "GlideDateTime" is not defined.
Caused by error in <refname> at line 1

==> 1: GlideDateTime(gs.minutesAgo(-10)).getDisplayValue();


OR

find_real_file.png

This shows up in the logs:
org.mozilla.javascript.EcmaError: "GlideDateTime" is not defined.
Caused by error in <refname> at line 1

==> 1: new GlideDateTime(gs.minutesAgo(-10)).getDisplayValue();


For security reasons we run some scripts in a sandbox. I'm assuming that's the case here and sandbox isn't allowed to access GlideDateTime object.

Here's another option using a client script that doesn't need a server trip. You'd have to adapt it to the template concept somehow depending on how you need these dates set.



//get the start value
var start = g_form.getValue("u_problem_start");

//get date in ms from the user's display format
var startDate = getDateFromFormat(start, g_user_date_time_format);
//OR get the current time
//var startDate = new Date().getTime();

//number of ms in 10 mins
var tenMins = 10 * 60 * 1000;

//create Date object for end
var endDate = new Date();
//set end Date
endDate.setTime(startDate + (tenMins));

//get end date in user format
var endDateFormatted = formatDate(endDate, g_user_date_time_format);

//set the end date in the form
g_form.setValue("u_problem_end", endDateFormatted);


Now that's some great outside of the box thinking that I should've come up with on my own. Thanks, John!