convert string to Glidedate

Arjun Arjun
Tera Contributor
  1. Hi experts,
  2.  
  3. I am Tring to insert the records from one table to another table in my personal instance, I have got struck with one of field called "Valid From"   which is string type and value of the filed is 'August 22, 2022' and i have same filed called "valid from" in other table which is Date/Time type.
  4. here I need to know is there way i can convert the string "August 22, 2022" to Date/Time and update in filed.
  5. I have tried with below script but no luck.
    1. var vt = current.valid_from;
    2.         var gdt1 = new GlideDateTime();
    3.         var format1 = 'MM-dd-YYYY ';
    4.         gdt1.setDisplayValue(vt + ' 23:59:59');
    5.         var dt1 = new GlideDate();
    6.         dt1.setValue(gdt1.getDate());
    7.         var fr = dt1.getByFormat('dd-MM-YYY');

    Thanks,

     

     

            
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Arjun Arjun 

yes you can convert

this will work

var vt = current.valid_from;
var gdt1 = new GlideDateTime();
var format1 = 'MMM dd, yyyy';
gdt1.setDisplayValue(vt + ' 23:59:59');
var dt1 = new GlideDate();
dt1.setValue(gdt1.getDate());
var fr = dt1.getValue();
gs.info(fr);

I tried the same in background script and it worked fine for me

Output:

 

date time conversion.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Arjun Arjun 

yes you can convert

this will work

var vt = current.valid_from;
var gdt1 = new GlideDateTime();
var format1 = 'MMM dd, yyyy';
gdt1.setDisplayValue(vt + ' 23:59:59');
var dt1 = new GlideDate();
dt1.setValue(gdt1.getDate());
var fr = dt1.getValue();
gs.info(fr);

I tried the same in background script and it worked fine for me

Output:

 

date time conversion.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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