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

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

Worked Thanks!