How to convert date and time into another time zone in scoped application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 01:25 AM
In below script include we are getting the location time zone and date of the location to be in office . Currently My Requirement is to convert the both location date(ld) and current datetime (nowDT) into location time zone i.e. getLOcationTimeZone . Both ld and now DT are in GMT time zone.
var getDate = Class.create();
getDate.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDateInfo: function() {
var getLocation = current.variables.office_location.time_zone; // Get the LOcation Time Zone
var locationDate = this.getParameter('sysparm_user_sysId'); // Get the Date of the Location on which the user wants to be in office
var ld = new GlideDateTime(locationDate); // GMT Time Zone
var nowDT = new GlideDateTime(); //GMT Time Zone
var dur = GlideDateTime.subtract(nowDT,ld);
var duration = dur.getNumericValue();
var durationSeconds = (duration/1000);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 11:37 PM
Hi David,
I am facing similar issue, Were you able to find the solution?
Thanks,
Anu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 11:43 PM
@Community Alums
can you post a new question for this and share the relevant scripts etc and tag me there?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 02:59 AM
Anu hi
I did get a solution - with the help of Servicenow Certification team.
We had to:
- have a Global Script Include
- deploy this within App as a "Fix Script"
- Customer Admin has to create Global Script include (from Fix Script content) as a post install step.
Then within Script Include we are able to :
getTimeFromConstraintWithDayNum: function(scheduled, dnum, tod, tzone) {
// get Scheduled time in intended timezone to
var time = new GlideDateTime(scheduled);
var tz = Packages.java.util.TimeZone.getTimeZone(tzone);
time.setTZ(tz);
time.addDaysLocalTime(dnum);
var d = time.getDisplayValue();
var nd = d.substring(0, 11) + tod + ':00';
var ntime = new GlideDateTime();
ntime.setTZ(tz);
ntime.setDisplayValue(nd);
return ntime;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 03:07 AM
Can you share how are you invoking it and with what parameters?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 08:23 AM
Ankur
The Script Include needs to have "Accessible from: All application scopes"
Then call something like:
var tzutil = new global.My_App_TimeUtil();
var utcTime = tzutil.getTimeFromConstraintWithDayNum('2023-06-26 16:00:00', 1, '18:00', 'America/Los_Angeles');
which gives
"2023-06-28 01:00:00"
To clarify this function - In our usage - Day 0 is defined as the on the calendar day of that timezone, so Day = 1 is next day @18:00 in that timezone - this function returns a GlideDateTime which can be stored .