Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to modify date format from mm/dd/yyyy to yyyy-mm-dd in onbefore transform script?

Rithu
Kilo Contributor

Hi,

 

Can i know how we can modify the date format from mm/dd/yyyy to yyyy-mm-dd in onbefore transform script. The target table has date field as a textbox. 

Thanks in advance.

 

7 REPLIES 7

What do you mean by incoming date here. 

Roger11
Giga Guru

I've found the getByFormat is unreliable, the best way to sort out date problems in your environment once and for all by creating your own date libraries to manage the transitions between local and system dates and format switching etc. I put this script together to catch the differences we were seeing in Australia early in the morning when the GMT date was different to our local date.

var myDate = new GlideDateTime();
gs.print(myDate);
gs.print(myDate.getDisplayValue());
gs.print(myDate.getDate() + '\n\n');

var fixDate = '2025-08-22';
var theDate = new GlideDateTime(fixDate);
gs.print(theDate);
gs.print(theDate.getDisplayValue());
gs.print(theDate.getDate() + '\n\n');

// how to make a GlideDateTime field "date-only" and have it work with date-only processing local or system time
var ds1 = myDate.getDisplayValue().split(' ');
var ds2 = ds1[0].split('-');

var newDate = new GlideDateTime(ds2[2] + '-' + ds2[1] + '-' + ds2[0]);
gs.print(newDate);
gs.print(newDate.getDisplayValue());
gs.print(newDate.getDate() + '\n\n');



*** Script: 2025-08-21 23:44:56
*** Script: 22-08-2025 09:44:56
*** Script: 2025-08-21
*** Script: 2025-08-22 00:00:00
*** Script: 22-08-2025 10:00:00
*** Script: 2025-08-22
*** Script: 2025-08-22 00:00:00
*** Script: 22-08-2025 10:00:00
*** Script: 2025-08-22

I'll add this to cover the LocalDate function too.
var myDate = new GlideDateTime();
gs.print(myDate);
gs.print(myDate.getDisplayValue());
gs.print(myDate.getDate() + ' —(SYSTEM DATE)');
gs.print(myDate.getLocalDate() + ' —(LOCAL DATE)\n\n');

*** Script: 2025-08-21 23:44:56
*** Script: 22-08-2025 09:44:56
*** Script: 2025-08-21 ---(SYSTEM DATE) *** Script: 2025-08-22 ---(LOCAL DATE)