- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
So i was needing to get the current date/time in a specific timezone the other day.
I know that you can easily generate it with the current user's timezone with either of these:
gs.info(gs.nowDateTime());
I know you can easily generate the current date in UTC with this:
- https://www.servicenow.com/community/itsm-forum/obtaining-datetime-values-based-on-a-specific-timezo...
- Problem here is that they are all using lots of package calls, which is kind of frowned upon when you don't have to
Then I've found other posts where they were doing a lot of math to try and generate it, but I wanted to avoid that because it seemed like overkill.
We do have the following GlideDateTime functions available on the docs site: https://docs.servicenow.com/bundle/utah-api-reference/page/app-store/dev_portal/API_reference/GlideD...
- GlideDateTime.setTZ(Timezone)
This has the following example:
var tz = gs.getSession().getTimeZone();
var gdt = new GlideDateTime();
gdt.setTZ(tz);
There is another/better option, and that's GlideDateTime().setTimeZone(), well, at least that's what I THOUGHT at first. I tried this code:
var gdt = new GlideDateTime();
gdt.setTimeZone('US/Eastern');
gs.info(gdt);
var gdt = new GlideDateTime();
gdt.setTimeZone('US/Eastern');
gs.info(gdt.getDisplayValue());
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0402714');
gr.setLimit(1);
gr.query();
if (gr.next()){
gs.info('Number: ' + gr.getValue('number'));
gs.info("UTC Time: " + gr.getValue('sys_created_on'));
gs.info("Curr User TZ: " + gr.getDisplayValue('sys_created_on'));
var tmpTime = new GlideDateTime();
tmpTime.setDisplayValue(gr.getDisplayValue('sys_created_on'));
gs.print("UTC again: " + tmpTime);
tmpTime.setTimeZone('US/Eastern');
gs.print("US/Eastern TZ: " + tmpTime.getDisplayValue());
}
- 29,494 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.