How to set mm-dd-yyyy value from string to date field

richam
Tera Expert

Hi all,

Need to some help to set value received in a string field with format mm-dd-yyyy to date field(not datetime field). Can anyone help here. I am setting this value in variable of catalog item.

Thanks in advance.

Richa

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here you go. use this in a server side code



var usr= new GlideRecord('sys_user');


usr.get(gs.getUserID());


var gd=new GlideDate();


  gd.setDisplayValue(your date value);


  current.<your date field name>=gd.getByFormat(usr.getValue('date_format'));


View solution in original post

5 REPLIES 5

drjohnchun
Tera Guru

Hi Richa - please try below; they both produce the same date:



gs.print(new GlideDateTime('2016-08-09').getDate());   // *** Script: 2016-08-09


gs.print(new GlideDateTime('08-09-2016').getDate());   // *** Script: 2016-08-09



EDIT: .getDate() returns GlideDate.


http://wiki.servicenow.com?title=GlideDateTime#getDate.28.29



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile


visit snowaid


Natsufab3
Kilo Guru

Hi Richa,


                First change the format of your default date. if it is YYYY-MM-DD change it to MM-DD-YYYY by going to system properties => System and there you can search for date format field and change it there.



And may be this thread may come in handy for rest



How to convert date format


User Date Format and .setValue (date / time) inconsistencies



Thank you


Abhinay Erra
Giga Sage

Here you go. use this in a server side code



var usr= new GlideRecord('sys_user');


usr.get(gs.getUserID());


var gd=new GlideDate();


  gd.setDisplayValue(your date value);


  current.<your date field name>=gd.getByFormat(usr.getValue('date_format'));


This works as expected,thanks Abhinay