Date format

Rishabh_N
Tera Contributor

In a catalog item, I have a date field where the format appears as dd/mm/yyyy for me, which is causing my script to not execute properly. However, when I impersonate another user, the date format changes to yyyy/mm/dd, and the script executes fine.

I checked System Properties, where the date format is set to yyyy/mm/dd, but for my account, it still shows dd/mm/yyyy. I’m not sure if I made any changes earlier.

Can someone help me understand why this is happening and how to fix it so that my date format also appears as yyyy/mm/dd?

Thanks in advance!

3 ACCEPTED SOLUTIONS

SN_Learn
Kilo Patron
Kilo Patron

Hi @Rishabh_N ,

 

Please check the user preference in your account:

SN_Learn_1-1741238280747.png

 

Under Language and Region:

 

SN_Learn_0-1741238238274.png

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

surajnikam111
Tera Guru
Tera Guru

Hi @Rishabh_N ,

You can use below script to format date field:

 

var date = new GlideDate();// replace new GlideDate() with the incoming date
var dateNew = date.getByFormat('yyyy/MM/dd'); // now use the dateNew to set the field value

 

Regards,

Suraj

View solution in original post

@Rishabh_N 

no scripting required. you can simply use UI policy and you need not worry about the date format

No Code date validations through (Catalog) UI Policies 

use this in UI policy condition

Your Date relative before 7 days from now

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

6 REPLIES 6

SN_Learn
Kilo Patron
Kilo Patron

Hi @Rishabh_N ,

 

Please check the user preference in your account:

SN_Learn_1-1741238280747.png

 

Under Language and Region:

 

SN_Learn_0-1741238238274.png

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

in the preferences i can only see dd-mm-yyyy format

Ankur Bawiskar
Tera Patron
Tera Patron

@Rishabh_N 

it depends on logged in user's date format.

what validation are you planning to run? you can tweak it to ensure it works in all formats.

share the business requirement and what's currently configured

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

The scenario is that the selected date should be at least 7 days from the current date. I have written a Script Include and a Client Script for this. However, even when I select a date within the allowed range, it still throws an error when the date format is dd/mm/yyyy. But when I impersonate another user, it works fine when the format is yyyy/mm/dd. this might be because dateDiff is only working correctly with the yyyy/mm/dd format.

 

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

    isDateInRange: function() {

        var selDate = new GlideDateTime(this.getParameter('sysparm_deldate'));
        var today = new GlideDateTime();

        var diff = gs.dateDiff(today, selDate, true);

        var isInRange = false;
        if (diff >= 0 && diff <= 604800) {
            isInRange = true;
        }
        return isInRange;
    },
    type: 'dateValidation'
});