How to change the date format on a specific field from short (MM/DD/YYYY) to long (MMM DD, YYYY)?

Pooja Reddy
Kilo Contributor

Hello,

I need to change the format of a specific date field on an HR case form from short (MM/DD/YYYY) to long (MMM DD, YYYY).For instance, 03/28/2108 should be March 28, 2018. I need to change the date format only for a single field and not across the system (I am on Kingston)

 

I tried creating a new System Property (long_date_format) and adding it to the Attribute in the Dictionary Entry of the field, but couldn't get it to work. I am thinking I might need to use a script include, but I am new to scripting. So before I try that, first wanted to check with you folks to see if there is an easier solution. If anyone could point me in the right direction, that would be really helpful!!

 

Many thanks!!

 

 

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage
  • Create a New Field called 'Long Date' of type String
  • Click 'Advanced View'

find_real_file.png

  • On 'Calculated Value' tab, check 'Calculated'

find_real_file.png

  • Insert the code below into the 'Calculation' field, substituting 'resolved_at' with your field
(function calculateLongDate(current) {
	var gDateTime = new GlideDateTime(current.resolved_at.getDisplayValue());
    var gDate = gDateTime.getDate();
	var longDateTime = gDate.getByFormat('MMMM dd,YYYY');
	return longDateTime;  
})(current);
  • Add the field to the form, under your Date field (example below)

find_real_file.png

Date/Time formats are documented here.

Similar to the Business Rule solution, it only updates on form load.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

12 REPLIES 12

erik_brostrom
Mega Guru

To my knowledge, date/time format is a system wide setting / user preference. I don't believe you'll be able to systematically change the display of this one field to appear as described (I could be wrong, and if I am someone please let me know); BUT ... this could work: create a new field (below the current field) that is a string and displays in the format you are talking about via a business rule. (would be just a string though, not a actual date/time field).

You would need to be a admin in your instance, if you are not already (if not, I'd consult with your admin).

 

Example below:

New Field called 'opened other format' [u_opened_other_format], string, 40 char

New Business rule:

When to run/table/other content in BR would depend on your use case..

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	
	var gdt = new GlideDateTime(current.opened_at);
	var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"];
	current.u_opened_other_format = monthNames[gdt.getMonthLocalTime()] + " " + gdt.getDayOfMonthLocalTime() + ", " + gdt.getYearLocalTime();

})(current, previous);

 

Upon save it then generates as below:

 

find_real_file.png

Probably not the optimal way to get it to work, but it is a option.

 

Hopefully that points you in the right direction or helps generate some ideas! 

 

Hi Erik,

 

Thanks a lot for this! 

 

Almost there! The Business Rule seems to work but the long date is a month ahead and a day behind the short date field. So 2018-04-03 displays as May 2, 2018 and not April 3, 2018. 

find_real_file.png

In your screenshot, I see that the day is the same but its a month ahead of the Opened date field.

 

Not sure why the BR is acting differently, as I am using the exact same code.

 

I'd be very grateful if you could look into this further when you have a moment.

 

Many many thanks!

Supriya Sirse1
Giga Expert

Hi Pooja,

I tried this with Business rule. Kindly check with below Script this may help you.

>> u_hr_date we have date which we need to convert. 

>> Write BR on Insert/update run on OnBefore 

>> Script

(function executeRule(current, previous /*null when async*/) {

var date_time= current.u_hr_date;
gs.addInfoMessage(date_time);
var date= date_time.split(' ')[0];
var gd = new GlideDate();
gd.setValue(date);
date=gd.getByFormat('MMMM DD,YYYY');
date_time=date;
gs.addInfoMessage(date_time);

})(current, previous);

>> Screenshot :

find_real_file.png

 

Kindly let me know in case you have any queries.

 

Regards,

Supriya S.

Kindly Hit  Helpful, Reply,Mark as Correct Answer if you found useful.

 

Hi Pooja,

 

Kindly use below modified script:

(function executeRule(current, previous /*null when async*/) {

var date_time= current.u_hr_date;
var gd = new GlideDate();
gd.setValue(date_time);
var date=gd.getByFormat('MMMM DD,YYYY');
date_time=date;
gs.addInfoMessage(date_time); // Printed Date format


current.short_description = date_time; // use a field where you want to store. I used                                                               ShortDescription for testing purpose


})(current, previous);

 

Kindly let me know in case you have any queries.

 

Regards,

Supriya S.