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

This is a script include function used in the record producer to update the date value selected by the user.

MMMM dd, YYYY <--- this is the date format that is displayed to users.

YYYY- MM-dd <--- this is the ServiceNow format

I have to change date format only for User purpose

Let me try your solution

Hi Kris,

Have you tried above script? Means user select MMMM dd, YYYY but automatically it should change to YYYY-MM-dd?

Thank you
Prasad

User selects MMMM dd, YYYY, but it doesn't automatically change to YYYY-MM-dd by ServiceNow

kris29
Tera Contributor

@Prasad Pagar, @Ankur Bawiskar 
I'm change to setValue because it doesn't work with setDisplayValue.
But it's very strange because it updates the records, but with date not selected

For example, the value of the field in the profile HR is set to 2021-06-01, I select July 27, 2021 from the select box and the field HR is updated to 2021-06-08.

updateEmplDate: function(producer, current) {
        var employee = producer.user;
        var date = producer.employment_date;
        var gdt = new GlideDate();
        gdt.setValue(date);
        var endDate = gdt.getByFormat("yyyy-MM-dd");
        var isRecordExist = new GlideRecord('sn_hr_core_profile');
        if (isRecordExist.get('user', employee)) {
            isRecordExist.employment_date = endDate;
            isRecordExist.update();
            current.setAbortAction(true);
        }

Hi,

what type of variable you are using?

Is it not date variable?

Can you share your form image?

Also I assume you are asking user to enter date and that will get stored in the HR Profile record?

Is it via some record producer? if yes then why not use record producer script for this

Regards
Ankur

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