split date & time

Little
Kilo Expert

Hi,

I want to split Date & time from below value and pass it into a date time filed in the format yyyy-MM-dd HH:mm:ss.

2022-03-24T23:59:59.000+00:00

 

Please help!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the value you are having is in ISO format

use this to get only date/time

Script:

var iso = '2022-03-24T23:59:59.000+00:00';

var date = iso.split('T')[0];

var time = iso.split('T')[1].split('.')[0];

var dateTime = date + ' ' + time;

gs.info(dateTime);

Output:

[0:00:00.062] Script completed in scope global: script


Script execution history and recovery available here


*** Script: 2022-03-24 23:59:59

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Alex Coope - SN
ServiceNow Employee
ServiceNow Employee

I've just checked this, and GlideDateTime does actually support this format:

var gDate = new GlideDateTime('2022-03-24T23:59:59.000+00:00');
gs.print(gDate);
*** Script: 2022-03-24 00:00:00


More info can be found here, so there shouldn't be any need to do splits etc,

Many thanks,
Kind regards

--------------------------------------------------------------------
Director of Globalization Deployment, Internationalization

Little
Kilo Expert

Hi All,

I am confused with the replies as if which 1 I should follow:

My main motive it to fetch the value from the field as follows. Expires field is a string type field.

find_real_file.png

After fetching, I need to pass it into a field as follows which is a Date/Time field.

find_real_file.png

 

So, I wanted to remove the T from the Expires value and also remove that 000+00:00, so that I can update the Valid To with Date/Time only.

Community Alums
Not applicable

You should follow the post from Alex

You should take the "expires" value, pass it into a new GlideDateTime object as a parameter and pass the result of that variable into your Valid to field. 

Something like: 

var gdt = new GlideDateTime(expiresValue);
record.setValue('valid_to', gdt);

@Little 

Did you face any challenge in the script shared by me or Mike?

what is not working?

Regards
Ankur

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