- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 11:37 AM
We are forwarding emails to ServiceNow and then programming an Inbound Email Action to update the User record based on the contents of the email. Working with dates and date comparison has always been a little tricky in ServiceNow/Java, especially when you have to convert them from text to date and then do a comparison on them.
In the body of the email, I will have a line of text that will look like this:
Termination Date 12/07/2023
It will always be the phrase "Termination Date" followed by a date in "mm/dd/yyyy" format.
In the Script for this Inbound Email Action, I need to compare this date to see if it is a future date (after today). I already figured out how to extract the date part of the string, like this:
//get values from email body
var ebody = email.body_text;
//get termination date string
var temp2 = ebody;
var b = temp2.indexOf("Termination Date");
temp2 = temp2.substr(b + 16, b + 30);
temp2 = temp2.trim();
temp2 = temp2.substr(0, 10);
so that temp2 returns "12/07/2023".
But now I need to compare this date to the current date and determine whether or not it is a future date.
Can someone help with that part of the script?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 12:08 PM - edited 12-07-2023 12:09 PM
So you're going to need to convert the date you have in the text to a GlideDateTime object in ServiceNow. Once you do that, you can compare it with the current date. First, let's create a GlideDateTime object for your termination date.
var terminationDate = new GlideDateTime('12/04/2023');
Next, we want to create a comparison to today's date (or any other date you may like).
var currentDate = new GlideDateTime();
Now that we have two date objects, we can compare them both using the compareTo() method.
var terminationDate = new GlideDateTime('12/04/2023');
var currentDate = new GlideDateTime();
var compareDates = terminationDate.compareTo(currentDate);
if (compareDates == -1) {
// The termination date has passed, process your code here.
} else {
// The termination date has not passed, process your code here.
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 01:08 PM
Thank you!
That works perfectly!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 01:08 PM
Awesome - happy to help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 03:11 PM
@jmiskey If you wanted to get fancy with your code you could slim it down to this, of course replace the hardcoded date value with your variable, and the print statements with the code you wish to execute.
new GlideDateTime('12/04/2023').compareTo(new GlideDateTime()) == -1 ? (function dateMet() {
gs.print('Date has been met');
})() : (function dateNotMet() {
gs.print('Date hasnt been met');
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 12:52 PM
Hello @jmiskey here is another approach
var temp2 = '12/8/2023'; // for testing only
// get today as a date object - so we ignore the time portion.
var gdt1 = new GlideDate();
gdt1.setToNow();
// parse original date so we can convert it to std format
var datearr = temp2.split('/');
var newdate = datearr[2] + '-' + datearr[0] + '-' + datearr[1] ;
var gdt2 = new GlideDate();
gdt2.setValue(newdate);
// compare dates
if ( gdt2.after(gdt1)) {
// conditional code here.
gs.log( 'it is past today ');
}
hope that helps
--
Bala
Please mark this as helpful or a solution depending it's utility