Custom Date Format

Aakanksha Kotti
Tera Contributor

Hi Team,

 

We have a requirement where a field should display 'Wednesday, May 10, 2023' whenever we select a specific date in another date field in a catalog item.

Kindly let me know how this can be achieved.

 

Thank you in advance.

1 ACCEPTED SOLUTION

Karan Chhabra6
Mega Sage

Hi @Aakanksha Kotti ,

 

You need to create an onChange client script and script include to achieve this, please refer to the below scripts

I am assuming that the field in which you need to populate the long date is of type string.

 

1. onChange catalog client script, on change of Date field

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var newDate = newValue;
    var dateGA = new GlideAjax('DateUtils');
    dateGA.addParam('sysparm_name', 'getFullDate');
    dateGA.addParam('sysparm_date', newDate);
    dateGA.getXMLAnswer(_response);

    function _response(answer) {

        g_form.setValue('<field_name>', answer);

    }

}

 

 

Step 2: Create a client callable script include and name it DateUtils, use this script

 

var DateUtils = Class.create();
DateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getFullDate: function() {

        var date = this.getParameter('sysparm_date');
        date = date.toString();
        var gd = new GlideDate();
        gd.setDisplayValue(date);
        var fullDate = gd.getDisplayValueLang("full");
        return fullDate;

    },

    type: 'DateUtils'
});

 

Example output: this is on incident form, it will behave the same on catalog form as well.

KaranChhabra6_0-1685534953787.png

 

 

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks,

Karan

View solution in original post

3 REPLIES 3

Karan Chhabra6
Mega Sage

Hi @Aakanksha Kotti ,

 

You need to create an onChange client script and script include to achieve this, please refer to the below scripts

I am assuming that the field in which you need to populate the long date is of type string.

 

1. onChange catalog client script, on change of Date field

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var newDate = newValue;
    var dateGA = new GlideAjax('DateUtils');
    dateGA.addParam('sysparm_name', 'getFullDate');
    dateGA.addParam('sysparm_date', newDate);
    dateGA.getXMLAnswer(_response);

    function _response(answer) {

        g_form.setValue('<field_name>', answer);

    }

}

 

 

Step 2: Create a client callable script include and name it DateUtils, use this script

 

var DateUtils = Class.create();
DateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getFullDate: function() {

        var date = this.getParameter('sysparm_date');
        date = date.toString();
        var gd = new GlideDate();
        gd.setDisplayValue(date);
        var fullDate = gd.getDisplayValueLang("full");
        return fullDate;

    },

    type: 'DateUtils'
});

 

Example output: this is on incident form, it will behave the same on catalog form as well.

KaranChhabra6_0-1685534953787.png

 

 

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks,

Karan

Hi @Karan Chhabra6 ,

Thank you so much for the script and your help. It worked!!

Much appreciated. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Aakanksha Kotti 

are you saying when user selects a date; that date has to be converted in the custom format?

can you explain with screenshots?

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