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 format date field for onBefore transform script

mdjoseph12
Giga Contributor

How can I run an onBefore transform script to convert mm-dd-yyyy hh:mm:ss to yyyy-dd-mm ? I attempted to use target.u_install_date = new GlideDateTime(source.u_asset_start_date) but it continued to show an error 

I've also tried: target.u_install_date = source.u_asset_start_date.getByFormat("yyyy-mm-dd")

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Joseph,

So incoming value is date/time and target field is date field; but the format is different try this script and let me know if that works

var dateTime = source.u_asset_start_date;

var gdt = new GlideDateTime(dateTime);

var year = gdt.getYearUTC();
var month = gdt.getMonthUTC();
var day = gdt.getDayOfMonthUTC();

var finalDate = year + '-' + day + '-' + month;
target.u_install_date = finalDate;

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

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

View solution in original post

6 REPLIES 6

purbalipc
Tera Expert

Hi

Try this

 

var date = new GlideDateTime();
date.setDisplayValue(source.u_asset_start_date, "dd/MM/yyyy"); // 2nd parameter is format of source data
target.u_install_date = date.getDate();

 

make sure date format field is the system date format

Unfortunately this didn't work

Got this to work by using: 

var date = new GlideDateTime(source.u_asset_start_date);
return date; // return the value to be put into the target field

 

But the problem I am running into is that the year is coming in as yy as opposed to yyyy, is there a way to modify this? 

Cody Smith _ Cl
Tera Guru

Hello MD Joseph,

I believe the issue here is that you are calling GlideDateTime from a client side script. What you should do is make a script include with your time conversion in it, and then call it from your transform script. 

 

If my answer was helpful, or solved your issue, please mark it as Helpful / Correct.
Thank you,
Cody Smith