Convert date format from YY-MM-DD to DD-MM-YYYY

pujitha K
Tera Contributor

Hi All,

 

In record producer we have one variable which is Date type, in employee center it is displaying as YY-MM-DD

pujithaK_0-1753966546831.png

 

I have requirement to change this from YY-MM-DD to DD-MM-YYYY in employee center form view and on the request information on the record should show in DD-MM-YYYY format.

 

I created script include and on change client script.

 

Script include.

var ValidfromCreateaNewCostCenter = Class.create();
ValidfromCreateaNewCostCenter.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
   
    convertDateFormat: function() {
        gs.log('Pu date1');

        var incomingDate = this.getParameter('sysparm_format');
        var format = 'yyyy/dd/MM';
        var gdt = new GlideDateTime();
        gdt.setDisplayValue(incomingDate, format);
        var gd = new GlideDate();
        gd.setValue(gdt.getDate());
        return gd.getByFormat("MM/dd/yyyy");

    },

    type: 'MyConversion'
});
 
On change Client Script on variable 
 
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

alert('new value ' + newValue);
 var ga = new GlideAjax('x_bcb_mdm.ValidfromCreateaNewCostCenter');
   ga.addParam('sysparm_name', 'convertDateFormat');
   ga.addParam('sysparm_format', 'newValue');
   ga.getXMLAnswer(function(response){

    alert('gp ' + response);

    var FormattedDate = response;
    g_form.setValue('variable_name', FormattedDate);
   });
 
I have tried with above scripts the logs in script include are not showing and in client script alert response is showing as null 
7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@pujitha K 

Why to convert the date format?

The date format is shown as per logged in user's date format.

what's your actual business requirement?

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

Hi @Ankur Bawiskar ,

 

This is the requirement I have received from client they wanted to make consistent across for all the users

 

@pujitha K 

that's what I informed, it can't be consistent as the date format depends on the logged in user's date format.

User can change the date format anytime from preferences so your script will fail.

AnkurBawiskar_0-1753980670947.png

 

Please inform your customer about this.

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

Hi @Ankur Bawiskar,

 

sorry my requirement is different only for particular variable catalog item they wanted to 

DD-MM-YYYY format

 

As soon as they open catalog item in portal, they want DD-MM-YYYY format for that particular variable, and it should stay same after submission also