Get a GlideDateTime object from string format yyyy-MM-ddTHH:mm:ss ±hh:mm

Daniel A-C
Tera Expert

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

1 ACCEPTED SOLUTION

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


View solution in original post

9 REPLIES 9

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


Hi Daniel,



Thank you for the kind words. I'm glad you got this resolved. Would you mind marking the appropriate response correct so others can find it quickly and it gets marked as "Answered"?



P.S. Some users do not see the Correct Answer link (per this video at 20:00 https://community.servicenow.com/thread/224893). Let me know if you do not see it and I can get a community moderation to assist.


Hi Chuck,



Your answer definitely pointed me in the right direction but I hope you don't mind that I've marked my own one as correct, as I've been able to successfully test it. Your example I think was doing the addition/subtraction the wrong way around for my needs.



Again, thank-you for your help!



Daniel


Not at all. Do what's best for the community, not my points tally.


Ramya30
Tera Contributor

Hi All, 

 

Are there any built-in functions available in Paris/Quebec to handle this Date Time conversion? I am having trouble populate a timestamp value in a payload from GitHub into a Date/Time field in ServiceNow via a Flow Designer. Any help here is truly appreciated. 

 

Thanks

Ramya