Script to change date format from email

Richard Thomas2
Tera Contributor

Hi all,

Is it possible to change the date format from:

8 September 2020

to

08/09/2020 - via a script? would then need to populate a date in a request item...

Thanks for your help

 

Regards

 

Rich

1 ACCEPTED SOLUTION

Hi,

Try like this.

var termination_date = email.body.termination_date; 
termination_date = termination_date.split(" ");
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];
//get the month
var mon = month.indexOf(termination_date[1].toString());
mon++;
var gdt1= new GlideDateTime();
gs.info("Date format is "+gdt1);
var dt = termination_date[2]+"-"+mon+"-"+termination_date[0]; //change to your system format here
var gdt = new GlideDateTime(dt);
//Now gdt contains the date in the SN format.

View solution in original post

13 REPLIES 13

Shri8
Tera Contributor

Hi Asif,

 

I used the same script but its not capturing exact date from email body to Servicenow.

Email format --

<<<BEGIN>>>
Type=Request - Service
Requested Resolution Date=Thu, 13 May 2021 11:25:56 -0400
<<<END>>>

 

Script used--

var termination_date = parseBody("Requested Resolution Date=");
termination_date = termination_date.split(" ");
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];
//get the month
var mon = month.indexOf(termination_date[1].toString());
mon++;
var gdt1= new GlideDateTime();
gs.info("Date format is "+gdt1);
var dt = termination_date[2]+"-"+mon+"-"+termination_date[0]; //change to your system format here
var gdt = new GlideDateTime(dt);
//Now gdt contains the date in the SN format.
gdt1.setDisplayValue(dt);
cart.setVariable(item, 'requested_resolution_date', gdt1.getDate());

 

Variable date format is yyyy-mm-dd.

Could you please suggest.

Thank you Asifnoor!

Hi Asif,

I'll get the date as "6-Apr-22" from CSV and I'm using transform map. In script I want to convert it to MM-dd-yyyy. I'm unable to convert the year from 22 to 2022.

Thank you,

Megha Padale
Giga Guru

Hi Richard,

Check below link, it might help you

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

If my answer helped you in any way, mark answer as helpful and correct.

Thanks and regards,

Megha