String date to date conversion

Rushi2
Tera Expert

I want to convert string date (2019-MAR-22)  to date format (03-22-2019). 

Regards, 

Rushi

1 ACCEPTED SOLUTION

Chander Bhusha1
Tera Guru

Hi Rushi,

Try the below script:

 

Script: 

var date = '2019-MAR-22';//Input Date

var gd = new GlideDate();
gd.setDisplayValue(date, "yyyy-MMM-dd");


gs.print('output   '+gd.getByFormat("MM/dd/yyyy"));//Output

 

 

Output of the srcript would be :

 

find_real_file.png

 

 

Please mark helpful and correct.

 

Thanks,

CB

View solution in original post

7 REPLIES 7

Priyanka Vasant
Tera Guru

Hi Rushi,

 

In addition @Namrata  find the below link that might help you.

https://community.servicenow.com/community?id=community_question&sys_id=bf9f0365dbdcdbc01dcaf3231f96...

 

Mark helpful or correct based on impact.

 

Regards,

Priyanka A.

Imam Pasha1
Giga Guru

Hi Rushi,

 

Please find the below working code as per your requirement.

 


var date = '2019-MAR-22'; // your input date
var simpleDateFormat = 'yyy-MMM-dd'; // Simple Date Time format
var gdt = new GlideDateTime();

gdt.setDisplayValue(date,simpleDateFormat); 
var localedt =gdt.getLocalDate();
var getStart= localedt.getByFormat('MM-dd-yyyy');  //this is your out put date format
gs.addInfoMessage(getStart); 

output:

input from user: 2019-MAR-22

output:03-22-2019

 

Hope this is helpful and please mark if you find the appropriate solution.

 

Regards,

Imam

manjusha
Kilo Guru

Hi,

var dateTime = '10 Jul 2018 12:30:10';
var dateFormat = 'dd MMMM yyyy HH:mm:ss';
var gDate= new GlideDateTime();

gdt.setDisplayValue(dateTime,dateFormat );
gs.print(gDate.getDisplayValue());

i tried above code in background  script and it is working correctly.

so try above code .

Mark this answer as correct if it helped.

Thanks,

Manjusha Bangale