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.

Set Date and Time to Next Day at Specific TIme

Marques2
Tera Expert

I'm attempting to set the date and time for a field on a catalog item. I'm calling it a "Need by Date". The requirement is to default the date and time 1 day out, and set the time to 7:00:00 that day. Can someone help configure this? I'm using a GlideDateTime (addDay(1)) and GlideTime (setValue) which is adding 7 hours to the current date/time.

Here's the script I'm using:

javascript:

var gdt = new GlideDateTime();

var gtime1 = new GlideTime();

gdt.addDays(1);

gtime1.setValue("07:00:00");

gdt.add(gtime1);

gdt.getDisplayValue();

Here is What's Happening:

If the current date and time were: 02-09-17 15:00:00

The catalog item form will load and display: 02-10-17 22:00:00

Any help is appreciated!

Thanks,

-Marques

11 REPLIES 11

Andy,



Actually, what you have above is what I need. I am trying to figure out two things:



Where do you set this javascript to run? Is this code plugged into a client script, dictionary, other??


How do you set the value of 'gdr' to a specific field (like u_notification_date)?



Thanks!


Hey matthew.magee,



I work with Andy and can answer your first question. I set this at the default value of the Date/Time field. Andy will have to speak on behalf of your other question.



-Marques


Hey Marques,


thanks for the quick reply.



Here's a snapshot of my dictionary and default value:



capture.jpg



Unfortunately when I load the CHG form, the field is blank


Update to my update:



I threw in some log statements:



javascript:


var gdt = new GlideDateTime();


gdt.addDays(1);


gs.log('GDT: ' +gdt);


var tmrwAtSeven = gdt.toString().slice(0,10) + " 07:00:00";


gs.log('GDT2: ' + tmrwAtSeven);


var gdr = new GlideDateTime(tmrwAtSeven);


gs.log('GDT3: ' + gdr);



All three log entries appear:



capture.jpg



For some reason, i just can't get the new time stamp to show up on the form, it is still blank.



Any suggestions?


Hey matthew.magee



Looks like you're missing a 'Get Display Value' at the end of the script.


Try adding this to the end: gdr.getDisplayValue();



Lastly, I think andypollino last paragraph in the post is important:



"Now GDR is tomorrow at 7 (GMT). You should adjust the 07 if you're not in GMT! For example, if you want gdr to be 7:00am in EST, 07:00:00 should be 14:00:00 instead. Then, if a user on the US's East Coast does gdr.getDisplayValue(), it should display tomorrow's date as YYYY-MM-DD 07:00:00"


find_real_file.png



I tested your script in my personal developer instance and it should work now- Although I tried it in the 'Planned Start Date' field


find_real_file.png




javascript:


var gdt = new GlideDateTime();


gdt.addDays(1);


var tmrwAtSeven = gdt.toString().slice(0,10) + " 07:00:00";


var gdr = new GlideDateTime(tmrwAtSeven);


gdr.getDisplayValue();



Hope that helps!


-Marques