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

Community Alums
Not applicable

Assuming that you never care about timezone and you're always wanting just the time/date you can pull it out directly... in this code, you'd want to put the gdt object into your field: 

var date = "2022-03-24T23:59:59.000+00:00"
var dateonly = date.substring(0,10);
var timeonly = date.substring(11,19);
var formattedDateTime = dateonly + " " + timeonly
var gdt = new GlideDateTime(formattedDateTime);

If you do care about the timezone (ie, you're going to get stuff in that isn't +00:00), you'll need to be a bit more complex about it. 

Hi,

 

I have just uploaded the output of the code.



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


Thanks,
Vikas

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

@Ankur Bawiskar 

I have a similar requirement, but mine has to be a dynamic value getting the created at field which is in UTC like above and autopopulate/update on the custom field on the incident table.

Could you help me how to pass the value dynamically?

var iso = '2022-03-24T23:59:59.000+00:00'; --> here it has pass the 'sys_created_at' value

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

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

var dateTime = date + ' ' + time;

gs.info(dateTime); --> 'dateTime' should be updated on the 'u_incident_occured' field on the incident table.

 

Any pointers would help me.