How to convert current date time into ISO ISO8601 format: yyyy-MM-dd'T'HH:mm:ss'Z

lucky24
Tera Contributor

Hi Team,

We have integration with third party tool, where I have to Send Time Range From- to in body.

In from time I have to send Current Date Time in ISO ISO8601 format: yyyy-MM-dd'T'HH:mm:ss'Z then

I have subtract 10 minutes into  current date time and have to pass in "to"

 

How can we achieve it?

Please Help me here

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So you want to subtract 10mins and then convert to ISO?

if yes then do this

var gdt = new GlideDateTime('your date time');
gdt.addSeconds(-600); // 10 mins is 600 seconds

var dt = gdt.getValue().split(' ');
var f = dt[0].split('-');
var t = dt[1].split(':');
var event = new Date(f[0], f[1]-1, f[2], t[0], t[1], t[2]);

var isoDate = event.toISOString();

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Hi,

it is adding extra zero; remove that and it would work

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 
Thanks for your answer but I'm having an issue with it, literally copy pasted your code but the time is not correct

Running query in background script my timexone being London/Europe so utc+1 but the time is coming out as 

2022-10-04T22:36:19.000Z

so this is 7 hours ahead of UTC, currently 16:36/15:36UTC


current UTC time is 

 

and I'm reluctant to just do as you have with the month as I'm not confident this will resolve itself when it goes past midnight or we enter a new year

var event = new Date(f[0], f[1]-1, f[2], t[0]-7, t[1], t[2]);

Hey Dave,

 

I am having the same trouble as you I believe, the split code seems to be setting the DateTime as PDT Timezone...

var crStart = current.getValue('start_date') gives 2022-10-08 18:00:00 - which is what i entered into the CR, FYI the system and user is set as GMT.

 

var crStartSplit = crStart.split(' ');
var crStartSplitD = crStartSplit[0].split('-');
var crStartSplitT = crStartSplit[1].split(':');
var cutoverStart = new Date(crStartSplitD[0], crStartSplitD[1]-1, crStartSplitD[2], crStartSplitT[0], crStartSplitT[1], crStartSplitT[2]);

 

gives Sat Oct 08 2022 18:00:00 GMT-0700 (PDT) - which appears to be defaulted to PDT

 

and therefore  var cutoverIsoStart = cutoverStart.toISOString() gives 2022-10-09T01:00:00.000Z

 

which is clearly incorrect.

@Dave Edgar / @Ankur Bawiskar I too face the same issue , do you have any solution for this issue?