- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 09:06 AM
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")
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 10:55 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 09:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 10:29 AM
Unfortunately this didn't work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 10:48 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2019 09:44 AM
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.