How to change date format to Month DD, YYYY

kris29
Tera Contributor

Hi,

How to change date format from YYYY-MM-DD to Month DD, YYYY (for instance May 07, 2021)?

find_real_file.png

script include function

        getEmployeeDate: function() {
        var grHRProfile= new GlideRecord('sn_hr_core_profile');
        grHRProfile.addQuery('user', gs.getUserID());
        grHRProfile.query();

        if (grHRProfile.next()) {
            var hrProfileData = {
                employment_end_date: grHRProfile.getValue('employment_end_date'),
                probation_end_date: grHRProfile.getValue('probation_end_date')
            }
            return JSON.stringify(hrProfileData);
        }

 

I found similar solution but it's implemented in calculated value
find_real_file.png

1 ACCEPTED SOLUTION

Hi Try this

getEmployeeDate: function() {
        var grHRProfile = new GlideRecord('sn_hr_core_profile');
        grHRProfile.addQuery('user', gs.getUserID());
        grHRProfile.query();
        if (grHRProfile.next()) {
        var gdt = new GlideDate();
        gdt.setDisplayValue(grHRProfile.getValue('employment_end_date'));
        var endDate = gdt.getByFormat("MMMM dd,YYYY");

        var gdt1 = new GlideDate();
        gdt1.setDisplayValue(grHRProfile.getValue('probation_end_date'));
        var probDate = gdt1.getByFormat("MMMM dd,YYYY");     
        
            var hrProfileData = {
                employment_end_date: endDate,
                probation_end_date: probDate,
            };

            return JSON.stringify(hrProfileData);
        }

Thank you
Prasad

View solution in original post

27 REPLIES 27

sorry @Ankur Bawiskar , I missed your reply

type of variable is Date type

"Also I assume you are asking user to enter date and that will get stored in the HR Profile record?" <--- Yes, correct


"Is it via some record producer? if yes then why not use record producer script for this" <--- script include function is called in record producer

 

that was the first calling script include without changing date format

var callSI = new HRScriptInclude().updateEmplEndDate()

 

right now I'm calling function shared by Prasad

var callSI = new HRScriptInclude().setDataFormat(producer, current)

 

Ok then lets try this

Create onchange catalog client script like this on employment_date field with below code

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var cdt = g_form.getValue('employment_date');
    var ga = new GlideAjax('Test1');
	ga.addParam('sysparm_name','type');
	ga.addParam('sysparm_abc',cdt);
    ga.getXML(testing);

    function testing(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('employment_date', answer);
		
    }
}

Script include

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

    type: function() {
        var pg = this.getParameter('sysparm_abc');
        var gdt = new GlideDateTime(pg);
        var gtime1 = new GlideTime();
        gtime1.setDisplayValue(pg);
        var formatDate = gtime1.getByFormat("yyyy-MM-dd");		
        return formatDate;

    }

});

Make sure you SI look like this

find_real_file.png

This will change the format on fly as soon as you change the field on record producer

Thank you
Prasad

@Prasad Pagar  I haven't tested script yet
But that solution modify only date format for record located on HR profile? 

Date format on record producer will remain the same?

 

Expected date format for Userfind_real_file.png

Expected format
After updating Hr Profile format via record producer

find_real_file.png

The script which I have provide will convert date from MMMM dd, YYYY to yyyy-MM-dd on record producer. I though that is what you want.

I guess your requirement is

1. When user select the date it should be in MMMM dd,YYYY

2. When user submit the record producer the HR profile should have yyyy-MM-dd 

Is that correct?

Thank you
Prasad

correct