get user preference date format

Akki1
Tera Contributor

Hi,

I want to get user preference date format and I have a form field called modified which is concat of u_last_update & u_updated_by. These two fields are not being displayed on the form I want to get user preference format and set it accordingly and use it on modified . Can I use onLoad script and use it? But how exactly I get the format

10 REPLIES 10

@Akki1 

where are you using this? i.e. where are you writing this? is it at dictionary level?

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

@Ankur Bawiskar yes in function definition

@Akki1 

then query sys_user table with logged in user's sysId and get the format and then convert it to that format

How is the value showing now and what are you expecting it to show?

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

@Ankur Bawiskar Can I directly do here in function field?If yes can you help with the script.

And also if user has set preference then the format is displayed on sys_user table else if someone has not set then the field is empty there.
Currently value is showing as its stored in backed dd-mm-yyyy. But if there is any other date field on form that is showing according to user preference. I want in the user preference format.

Amit Gujarathi
Giga Sage

HI @Akki1 ,
I trust you are doing great.
To achieve this, you can use an onLoad client script. Here's an example of how you can accomplish this:

function onLoad() {
  // Get the current user's preferences
  var userDateFormat = g_user.date_format;

  // Retrieve the "modified" field on the form
  var modifiedField = g_form.getControl('modified');

  // Apply the user's date format to the "modified" field
  modifiedField.setDisplayValue(userDateFormat);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi