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

Ok In that case, I believe you dont need above client script and script include as its changing format for user. 

Lets have the same format which you are initially getting with my script

Now in Runscript of Record Producer just add below code

var gdt = new GlideDateTime(producer.employment_date);
var gtime1 = new GlideTime();
gtime1.setDisplayValue(producer.employment_date);
var formatDate = gtime1.getByFormat("yyyy-MM-dd");
current.employment_end_date = formatDate;

Thank you
Prasad

The previous script doesn't work, I modified it but it still doesn't work

@Prasad Pagar, @Ankur Bawiskar could you take a look
I've got an error

find_real_file.png

var employee = producer.user;
var date = producer.employment_end_date;
var gdt = new GlideDateTime(date);
var gtime1 = new GlideTime();
gtime1.setDisplayValue(producer.employment_end_date);
var formatDate = gtime1.getByFormat("yyyy-MM-dd");

var isRecordExist = new GlideRecord('sn_hr_core_profile');
if (isRecordExist.get('user', employee)) {

    isRecordExist.employment_end_date = formatDate;
    isRecordExist.update();
    current.setAbortAction(true);
}

Hi,

Did you check the record is present or not for that user in HR profile table?

Regards
Ankur

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

yes, user is present in HR profile table

kris29
Tera Contributor

Massive thanks to both of you @Ankur Bawiskar, @Prasad Pagar for your patience and support

 

I'll move my second question to new post