Change Date format from Variable Using Business Rule

Chris_Wilkinson
Tera Expert

I am trying to auto populate the short description field on the requeted item table using information from variables submitted via the service portal. This is being done using the business rule below

"(function executeRule(current, previous /*null when async*/) {
//On before insert business rule
//Condition where item contains Onboarding
//Updates the short description with concatenated variables
    
    current.short_description = current.variables.var_company.getDisplayValue() + ' ' + 'Onboarding' + ' / ' + current.variables.var_full_name + ' / ' + current.variables.var_start_date +' / ' + current.cat_item.getDisplayValue();
    
})(current, previous);

"

The rule works well except tha the date time variable (var_start_date) is being displayed in the wrong format. Our business have asked if the format can be changed to be  "DD-MM-YYYY" rather than the default "MM-DD-YYYY"

Is it possible to this within the business rule?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Chris W 

yes you can convert that

update as this and let us know.

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

	var startDate = current.variables.var_start_date;

	var gdt = new GlideDateTime();
	var format = 'MM-dd-YYYY HH:mm:ss';
	gdt.setDisplayValue(startDate + ' 23:59:59');

	var dt = new GlideDate();
	dt.setValue(gdt.getDate());
	var myFormattedStartValue = dt.getByFormat('dd-MM-YYY');

	current.short_description = current.variables.var_company.getDisplayValue() + ' ' + 'Onboarding' + ' / ' + current.variables.var_full_name + ' / ' + myFormattedStartValue  +' / ' + current.cat_item.getDisplayValue();

})(current, previous);

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Hi

in case your variable is really of type GlideDateTime you can get another format by something like this:

var date = '04 May 2020 12:58:00'; //replace with your variable
var simpleDateFormat = 'dd MMMM yyyy HH:mm:ss'; // Simple Date Time format
var gdt = new GlideDateTime();

gdt.setDisplayValue(date,simpleDateFormat); //Set time using current TZ

var newFormat = gs.addInfoMessage(gdt.getDisplayValue()); // get date in new format

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Ankur Bawiskar
Tera Patron
Tera Patron

@Chris W 

yes you can convert that

update as this and let us know.

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

	var startDate = current.variables.var_start_date;

	var gdt = new GlideDateTime();
	var format = 'MM-dd-YYYY HH:mm:ss';
	gdt.setDisplayValue(startDate + ' 23:59:59');

	var dt = new GlideDate();
	dt.setValue(gdt.getDate());
	var myFormattedStartValue = dt.getByFormat('dd-MM-YYY');

	current.short_description = current.variables.var_company.getDisplayValue() + ' ' + 'Onboarding' + ' / ' + current.variables.var_full_name + ' / ' + myFormattedStartValue  +' / ' + current.cat_item.getDisplayValue();

})(current, previous);

Regards
Ankur

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

Hi Ankur,

 

Thats works a treat 🙂

Thanks for the assist.

 

Kind Regards

Chris

Need to know where i gone wrong

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

var startDate = current.event_start_date;

var gdt = new GlideDateTime();
var format = 'YYYY-MM-dd HH:mm:ss';
gdt.setDisplayValue(startDate + '11:59:00');

var dt = new GlideDate();
dt.setValue(gdt.getDate());
var myFormattedStartValue = dt.getByFormat('MM/DD/YYYY');

current.short_description = current.variables.var_company.getDisplayValue() + ' ' + 'Onboarding' + ' / ' + current.variables.var_full_name + ' / ' + myFormattedStartValue + ' / ' + current.cat_item.getDisplayValue();

})(current, previous);