sabell2012
Mega Sage
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    BEGINNER
Assumes good beginner level knowledge and/or familiarity of Scripting in ServiceNow.


I get asked about this a lot: How do I get local time out of GMT (Greenwich)? Or, why is my day of week calculation not working?

 

The following is an example of how to obtain the corrected (from GMT) local day-of-week.

 

TEST: Expected results:   Day-of-week: 3.   Without the local offset it would be 2 (nasty GMT!).

 

var startDateTime = "2023-10-04 01:00:00";   // my example is in GMT
gs.info('---> starting date/time (in GMT): ' + startDateTime);

// The following returns the GMT date time object (converted from the string value).  
// So next step is to get the local offset and add it to GMT
var gmtDateTime = new GlideDateTime(startDateTime);  
gs.info('---> converted to GlideDateTime: ' + startDateTime.toString());

// getTZOffset is returned in negative milliseconds...
// so you have to get rid of that (I suppose you could use Math.abs)
var correctedOffset = new GlideTime(gmtDateTime.getTZOffset() * -1);   // the secret sauce
gs.info('---> converted to Corrected Offset Value: ' + correctedOffset.toString());

// NOW you can add the corrected value to the original gmtDateTime
gmtDateTime.add(correctedOffset);   // adds the milliseconds value to the gmt date/time object
gs.info('---> new timezone corrected Date/Time: ' + gmtDateTime.toString());

// true local day of week value
var dayOfWeek = gmtDateTime.getDayOfWeek();  
gs.info('---> true Day of the Week: ' + dayOfWeek.toString());

 

This is a painful method, but it gets the correct value for local day-of-week.

 

This script will produce the following results:

*** Script: ---> starting date/time (in GMT): 2023-10-04 01:00:00
*** Script: ---> converted to GlideDateTime: 2023-10-04 01:00:00
*** Script: ---> converted to Corrected Offset Value: 1970-01-01 05:00:00
*** Script: ---> new timezone corrected Date/Time: 2023-10-04 06:00:00
*** Script: ---> true Day of the Week: 3

 

Millisecond Converter

 

So, in the same vein, another handy/quick conversion to get days from milliseconds is:

 

var millConvert = 24 * 60 * 60 * 1000;   // hr, min, sec, milliseconds
var millisecondsExample = 604800000;   // 7 days expressed in milliseconds
var getDaysFromMilliseconds = millisecondsExample / millConvert;   // you get 7
gs.info('---> Days from Milliseconds result: ' + getDaysFromMilliseconds);

 

Results:

*** Script: ---> Days from Milliseconds result: 7

 

Just remember ye old programmer axiom: "Date/Times are evil!"

 

BTW, I would be curious to know if anyone has a faster/neater/cheaper method for the date/time correction!   Date/Time handling is always a super contentious topic of discussion so your responses are welcome! 🙂

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_1-1696102033098.png


Originally published on: 07-22-2015 07:02 AM

I updated the code and brought the article into alignment with my new formatting standard.