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.

Remove Time from Date Field for Catalog Client Script

jmiskey
Kilo Sage

I am working on creating a Catalog Item.  One of the Variables is "Due Date".  I need to adding checking/validation to this field so that the user cannot enter any date prior to the next business day.  So since today is Friday 8/25/2023, the earliest date they could choose for this field is Monday 8/28/2023.

 

I found some code in the community we can reference our "Weekdays excluding holidays" schedule, and if we tell it to go 24 hours, it goes to the next Business day.  The issue is, it calculated a time component along with it, and we do not want that.  We just want the date portion.  The Variable in the Catalog Item is a straight date field, with no time component.  We just want to make sure that the day they enter is not before the next business day.

 

Here is the code I have.  I am currently just running it in a background script to see what it returns.  And it returns 8/28/2023 along with a time component:

var gdt =  new GlideDateTime();
var dc = new DurationCalculator();
dc.setSchedule('8746beb20ffb9a00e3b522d8b1050ebf'); //Weekdays excluding holidays schedule
dc.setStartDateTime(gdt);
dc.calcDuration(24*3600); //1 business day = 24 hours
	
var edt= new GlideDateTime(dc.getEndDateTime())
edt.getDate();
gs.print(edt);

Does anyone know how I can accomplish this task, of creating this Catalog Client Script to verify the date entry?

 

If it is not possible to remove the time piece of my calculated date, maybe I can take the date entered and add 23:59 to it, and use that for comparison purposes (so  it would be taking the entered date and making it 11:59 PM on that day, so the comparison will work properly).

 

Thanks

1 ACCEPTED SOLUTION

piyushsain
Tera Guru

Hi,

In the background script you can try this

 

 

var edt= new GlideDateTime(dc.getEndDateTime());
edt = edt.toString().split(' ');
var date = edt[0];
gs.print(date);

 

Do the same in client callable Script Include and return the date and use in Catalog Client Script
 
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

View solution in original post

5 REPLIES 5

piyushsain
Tera Guru

Hi,

In the background script you can try this

 

 

var edt= new GlideDateTime(dc.getEndDateTime());
edt = edt.toString().split(' ');
var date = edt[0];
gs.print(date);

 

Do the same in client callable Script Include and return the date and use in Catalog Client Script
 
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

I see that is converting it to a string.  Is that going to present a problem if I then use it in a comparison against a date variables?

you can try it with a string, but if problem with string occurs , create the string back to date

example : 

new GlideDate(string);
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

This last update returns an error, but when I went back and tested your original solution, it worked!  I am a little surprised, as I thought it would return a String value.  Not sure I understand why that doesn't seem to matter, but I am happy it works, whatever the reason is!

 

So thank you!!!