How to remove characters from a string

Community Alums
Not applicable

Hi Team,

 

I have a string 2022-03-22 06:03:51 PDT

I need to get the output as 2022-03-22 06:03 PDT

Please let me know how to achieve this?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it seems you want to remove seconds

simply do this

var str = '2022-03-22 06:03:51 PDT';

var time = str.split(' ');

var arr = time[1].toString().split(':');

var hour = arr[0];
var mins = arr[1];

var finalString = time[0] + ' ' + hour + ':' + mins + ' ' + time[2];

gs.info("Final string->" + finalString);

Output:

find_real_file.png

Regards
Ankur

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it seems you want to remove seconds

simply do this

var str = '2022-03-22 06:03:51 PDT';

var time = str.split(' ');

var arr = time[1].toString().split(':');

var hour = arr[0];
var mins = arr[1];

var finalString = time[0] + ' ' + hour + ':' + mins + ' ' + time[2];

gs.info("Final string->" + finalString);

Output:

find_real_file.png

Regards
Ankur

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

Community Alums
Not applicable

Hi Ankur,

Thanks.

 

But if I want to extract the same from sys_updated_on then how to do so?

The below script is not working. 

Can you assist?


time = gr.sys_updated_on;
tme = time.split('');
arr = tme[1].toString().split(':');
hour = arr[0];
min = arr[1];
finaltime = tme[0] + ' ' + hour + ':' + min + ' ' + tme[2];
gs.print(finaltime);

 

BR,

Ankur