Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to set the Date Format

msm4
Mega Guru

Hi,

I have written some Validation to the Date Field , i.e, The Date field should be filled by default on "Onload"   with some specific Date.

Everything is working fine except the Date Format. OnLoad the Date format is "YYYY_MM_DD" but on selection of any date, the format is different "DD_MM_YYYY".

I want the default Date format in "DD_MM_YYYY", how to set that , Can anyone please help me out.

Thanks in Advance.

BR.

Smitha

1 ACCEPTED SOLUTION

Since GlideDate() as well as GlideDateTime() always use yyyy-mm-dd, you can change the format of your return value of dat by just adding .getDisplayValue():



return dat.getDisplayValue();



That will give you the format of whatever the user has even if they are using something else such as mm-dd-yyyy


View solution in original post

9 REPLIES 9

ccajohnson
Kilo Sage

Are you populating your date field via script, or by a default value on the dictionary? Knowing how the date is set should assist in how we can answer your question.


Yes, I am populating the Date via script. Find the below snippet:


Script Include:



var getdate = Class.create();


getdate.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getdatevalue : function()


  {


  var dat = new GlideDate();



  dat.addDaysUTC(5);



  return dat;


  },


type: 'getdate'


});



client Script:



function onLoad() {


    //Type appropriate comment here, and begin script below


    var ga = new GlideAjax('getdate');


  ga.addParam('sysparm_name','getdatevalue5');


  ga.getXML(callback);


}


function callback(response)


{


var answer = response.responseXML.documentElement.getAttribute('answer');


 


g_form.setValue('end_date',answer);


}


Since GlideDate() as well as GlideDateTime() always use yyyy-mm-dd, you can change the format of your return value of dat by just adding .getDisplayValue():



return dat.getDisplayValue();



That will give you the format of whatever the user has even if they are using something else such as mm-dd-yyyy


So basically dat.getDisplayValue() will return default or if set in user preferences, user preferred date format?



Sweet.