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

andypollino
Mega Expert

Marques,



Combining pieces of what you have with some JavaScript string manipulation and GlideDateTime's instantiation method hopefully will do what you want, even if it seems a little janky. Try this out:



javascript:


var gdt = new GlideDateTime();


gdt.addDays(1);


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


var gdr = new GlideDateTime(tmrwAtSeven);




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



EDIT: I accidentally called the 'slice' method 'splice' when I first posted this. That's not a string method, and won't get you what you want!


Hello!



I have a very similar problem, but in my case instead of current date, I have a start_date and end_date variable.


Is there a way to modify this script to show the end_date as the value provided in start_date + 1 day?




Thanks so much for help!


Joanna,



You will probably want a client script that runs as an onChange on the start_date variable. The actual script would probably look similar to this:



find_real_file.png



We use the JS Date() structure because GlideDateTime behaves a little strange. This script is written for two Date/Time variables, and it first gets the date object for the date entered in start_date (line 7). Line 8 sets the date to tomorrow, and line 9 adjusts from UTC to your current time (I'm in US Central Time which is UTC-6 so I have to subtract 6 hours because the Date object works in UTC). Line 10 formats the Date object in a convenient format (YYYY-MM-DDTHH:MM:SSotherstuff), so line 11 can interpret that string and format it for you.



You'll have to play around with formatting a little bit if your instance isn't set up in YYYY-MM-DD format!


With small modification to the format, I got exactly what I needed, thank you Andy!!