Convert String to Date

Richard Tamparo
Mega Guru

Im so sorry but I have been searching for hours now so i need help.

 

How do you convert YYYY/MM/DD string into a date with YYYY-MM-DD format?

2 REPLIES 2

Ahmmed Ali
Mega Sage

Hello @Richard Tamparo 

 

If you have value in string type variable, then you can simply replace / with - as below:

 


var oldFormat = '2014/10/22';
var newFormat = st.replace(/\//g, "-");

 

Thanks,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Manmohan K
Tera Sage

Hi @Richard Tamparo 

 

You can pass date in string format in below code and get date

var search = '/';

var replaceWith = '-';

var dateStr='2023/06/15';

var result = dateStr.replaceAll(search, replaceWith);

var gdt= new GlideDateTime(result);

var date= gdt.getDate();

gs.print(date);