Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

I want to use gs.beginningOfToday() for a specific timezone.

KS18
Giga Guru

I want to use gs.beginningOfToday() for a specific timezone.
I want to use javascript:gs.beginningOfToday()@javascript:gs.endOfToday() in a script to get the records created today.
As far as I know, the timezone for javascript:gs.beginningOfToday()@javascript:gs.endOfToday() depends on the timezoneof the user running that script.
If I want to get it in a specific timezone regardless of the user's timezone, what do I need to do?

9 REPLIES 9

Hitoshi Ozawa
Giga Sage

Sample code:

getBeginningDateTime("Asia/Tokyo");
getBeginningDateTime("America/Los_Angeles");
getBeginningDateTime("America/Chicago");

function getBeginningDateTime(timezone) {
  var today = gs.beginningOfToday();
  var gdt = new GlideDateTime(today);

  var tz = Packages.java.util.TimeZone.getTimeZone(timezone);
  gdt.setTZ(tz);
  var tzDiff = gdt.getTZOffset() /1000/ 60/60;
  gs.info( gdt.getDisplayValue() + " " + timezone+ " "  + tzDiff);
}

Execution result:

*** Script: 2022-02-09 00:00:00 Asia/Tokyo 9
*** Script: 2022-02-08 07:00:00 America/Los_Angeles -8
*** Script: 2022-02-08 09:00:00 America/Chicago -6

Thank you for your comment.
I get the following error when I try to implement this in a scoped application script.

java.lang.SecurityException: Use of Packages calls is not permitted in scoped applications
Caused by error in sysauto_script

Can I use this script only in Global?

Calling java package is only allowed in Global. That is, prefix the class name with "Global."

Try the following.

var today = new global.DateTimeUtil10().getBeginningOfToday();

Thank you for you comment, but it's not working in my environment.