- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2016 04:06 AM
Hi guys,
I have an integration returning me a date time string in the format yyyy-MM-ddTHH:mm:ss ±hh:mm and I would like to know how to get this into a GlideDateTime object so that I can accurately set it in a field on a GlideRecord.
E.g. a sample value returned
2016-07-16T16:15:23+01:00
So as I understand it, the UTC equivalent (and value written to the SN database) of this should be 2016-07-16 15:15:23. My user's timezone is Europe/London, I am in the UK and we are currently in DST (British summer time), so when I view it through the normal ServiceNow interface, I should see it as 2016-07-16 16:15:23.
I tried the standard method of new GlideDateTime(string) but it only picks up the date portion, has no reference to time or understanding of timezone.
I tried the function found here but the time is appearing to be two hours ahead of where it should be when viewed on the resulting GlideRecord.
I'm sure this is a common request but I can't seem to figure it out. What method should I be using to get this into a GlideDateTime object? There must be one as this is an internationally recognised ISO date format (8601:2004)
Thanks in advance for your help
Daniel
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 03:33 AM
Hi Chuck,
Thanks very much for your reply. I've used your example and combined it with the one I found earlier. Here's the resulting function, I hope it helps someone else...
/*
_ISODateTimeToGlideDateTime
- Returns a GlideDateTime object from an ISO8601:2004 datetime string provided in one of the following two formats:
- Format 1: yyyy-MM-ddTHH:mm:ss ±hh:mm (E.g. 2016-07-13T16:15:23+01:00)
- Format 2: yyyy-MM-ddTHH:mm:ss (E.g. 2016-07-13T16:15:23)
- Note on format 2: If no offset information is given or if the offset is given as Z (zulu time), it is assumed to be UTC
*/
_ISODateTimeToGlideDateTime : function (isoDateTime) {
gs.log('_ISODateTimeToGlideDateTime called with: ' + isoDateTime);
if ( (isoDateTime == '') || (isoDateTime == null) ) {
return null;
}
// GlideDateTime constructor expects date format: 'yyyy-MM-dd HH:mm:ss'
var parts = isoDateTime.split(/[-TZ:+]/g);
if (parts.length < 6) {
return null;
}
var dts = '';
dts += parts[0] + "-" + parts[1] + "-" + parts[2];
dts += ' ';
dts += parts[3] + ":" + parts[4] + ":" + parts[5];
// GlideDateTime object
gdt = new GlideDateTime(dts);
//Calculate timezone offset - if supplied
if ((parts[6] != null) && (parts[7] != null)) {
//determine offset amount
var offsetTime = new GlideTime();
var tzOffset = parts[6] + ':' + parts[7] + ':00'; // hh:mm:ss
offsetTime.setValue(tzOffset);
//remove offset by addition or subtraction
var sign = /\d\d-\d\d:\d\d$/.test(isoDateTime)? '-' : '+';
//if sign is + then remove time to get us back to UTC
if (sign == '+') gdt.subtract(offsetTime);
//if sign is - then add time to get us back to UTC
else if (sign == '-') gdt.add(offsetTime);
}
gs.log('_ISODateTimeToGlideDateTime returning GlideDateTime with internal value (UTC by default):' + gdt.getValue());
return gdt;
}
//TESTING OUTPUT...
gs.print(this._ISODateTimeToGlideDateTime('2016-07-13T16:15:23+01:00'));
//output: a GlideDateTime object with internal value of '2016-07-13 15:15:23'
gs.print(this._ISODateTimeToGlideDateTime('2016-07-13T16:15:23-01:00'));
//output: a GlideDateTime object with internal value of '2016-07-13 17:15:23'
gs.print(this._ISODateTimeToGlideDateTime('2016-07-13T16:15:23Z'));
//output: a GlideDateTime object with internal value of '2016-07-13 16:15:23'
gs.print(this._ISODateTimeToGlideDateTime('2016-07-13T16:15:23'));
//output: a GlideDateTime object with internal value of '2016-07-13 16:15:23'
gs.print(this._ISODateTimeToGlideDateTime('2016-07-13T16:15:23+01:00'));
//output: a GlideDateTime object with internal value of '2016-07-13 15:15:23'
The testing I've done so far suggests that this method works but I'm keen to hear any feedback you may have
Hey Chuck, just so as you know, I really enjoy your tutorial videos!! Always a pleasure to watch 🙂
Thanks all,
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2016 05:00 AM
Hi,
I can provide you some functionality to change the value to GlideDateTime format :
var maindate='2016-07-16T16:15:23+01:00';
var date1=maindate.split('T');
var d=date1[0];
var t=date1[1].split('+');
var finaldate=d+" "+t[0];
gs.print(finaldate);
O/P : 2016-07-16 16:15:23
Thanks and Hope it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2016 05:09 AM
Thanks for taking the time to reply Akhil.
This doesn't look like it returns a GlideDateTime object, so I assume your example is intended to have return new GlideDateTime(finaldate); as the final line.
If I am reading your script correctly, you are basically ignoring the timezone offset information and treating it as local time. I'm not sure that will work for me, but I'll give it a go.
Also, what would happen if a -08:00 was received as the time offset value? It looks like you are string searching for a '+' sign to do the split.
I'll try it anyway and see if it returns me what I'm looking for.
Thanks again.
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2016 06:05 AM
Hi Daniel,
Firstly, the output received is in GlideDateTime object.
Check below link for more info : GlideDateTime - ServiceNow Wiki
Secondly, it is a sample to show how you can convert and even you can use '-' as offset, the value returned would be the same as O/P.
Note: The above script is only extracting the date and time from the given format yyyy-MM-ddTHH:mm:ss ±hh:mm to GlideDateTime format.
Thanks.
Akhil

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2016 06:38 AM
Ahkil's script basically converts your format in to an acceptable format for GlideDateTime() to create an object. There's a bit more if you want to add the timezone offset. Here's a quick script I threw together to help you get the idea. See the GlideDateTime on the https://developer.servicenow.com site for complete reference.
var str = '2016-07-16T16:15:23-2:00';
var tArr = str.split('T');
var tPart = tArr[1].split('+');
var offSet = '+';
if (!tPart[1]) {
tPart = tArr[1].split('-');
offSet = '-';
}
var gdtStr = tArr[0] + ' ' + tPart[0];
gs.print('date=' + tArr[0] + ' time=' + tPart[0] + ' offset=' + offSet + tPart[1]);
var gdt = new GlideDateTime(gdtStr);
gdt.setValueUTC("2016-07-16 16:15:23", "yyyy-MM-dd HH:mm:ss");
gs.print(gdt.getValue());
var gtime1 = new GlideTime();
var tzOffset = offSet + tPart[1] + ":00";
gtime1.setValue(tzOffset);
gdt.add(gtime1);
gs.print(gdt.getValue());