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

Hi,

So are you passing the date value in this format to script include MMMM dd, YYYY?

Also you expect the script include should return in YYYY-MM-dd format?

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try to update as this

Also ensure you get the dates correctly

var gdt = new GlideDate();
gdt.setValue(grHRProfile.getValue('employment_end_date'));
var endDate = gdt.getByFormat("MMMM dd,YYYY");

var gdt1 = new GlideDate();
gdt1.setValue(grHRProfile.getValue('probation_end_date'));
var probDate = gdt1.getByFormat("MMMM dd,YYYY");     

if (grHRProfile.next()) {
    var hrProfileData = {
        employment_end_date: endDate,
        probation_end_date: probDate,
    };

Regards
Ankur

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

Hi Ankur,

Issue is 'grHRProfile' used before going into record.

Just corrected my code.

Thank you
Prasad