Converting A String In A Field To A Date & Outputting To Another Field

Anita Parrott
Kilo Contributor

Hi All

I have a date that I'm reading in from an email input, stripping the rest of the email from around it and trimming out spaces.  Cecause the date has come from an email it is in string format and I have placed the output from the trim etc in to a string field (New Contractor End Date Text field in the screen shot below).  As its from a user input it could be in any format.

What I need to do is convert that string date in the field New Contractor End Date Text in to an actual date in the right format (ServiceNow format) and output to the field New Contractor End Date.

find_real_file.png

I've tried a few GlideDate scripts but nothing seems to be working for me.  I've also read about Date Parse, but unsure if I need to use that.

Here is my last script:

var date = 'new_cont_end_date_text';
var simpleDateFormat = 'dd-mm-yyyy'; // Simple Date Time format
var gdt = new GlideDate();
gdt.setDisplayValue(date,simpleDateFormat);
gs.addInfoMessage(gdt.getDisplayValue());
u_new_contractor_end_date = setValue.gdt.getDate();

Can someone help me as to where I'm going wrong and am I making this more complicated than it needs to be?

Thanks in advance.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Anita,

yep this makes sense - but still, add to new GlideDateTime("12-12-2020 00:00:00") or it wont be very correct hence prone to errors.

Afterwards, on setting u_new_contractor*** use this format :

current.setDisplayValue('u_new_contractor_end_date', gdt.getDate();

 

Let me know how it goes

View solution in original post

6 REPLIES 6

Sorted!  I added it in to my workflow instead and its working.  Thanks for your help.

Community Alums
Not applicable
Great ! Glad you made it work. J