Calculating date in a template

sam_hull
Kilo Expert

Hello,

We have upgraded our dev instance to Eureka from Berlin and so far only have had a few minor issues. One of which I can't seem to fix so I was hoping someone here would have an idea. One of our teams uses a template for Change Requests. In this template, they have always set the "planned start date" and "planned end date" using


javascript:gs.now()

 

In Berlin, this would actually set the date and time to the current day at midnight which they liked. I think it only did this because gs.now() is a date function, but the field is date/time so it would default to midnight. Since the upgrade to Berlin, if we use gs.now(), it defaults to GMT time. So it doesn't look like midnight even though it is. I've found that


javascript:Packages.com.glide.glideobject.GlideDateTime()

 

gets the current date/time and shows it for the current time zone. We could get used to this if we have to, but we would love to be able to set it to show at midnight again in the current time zone, not GMT.

 

The other issue is that since the upgrade, it doesn't allow both fields to be the same time. It won't let us submit the change because it's validating that the end date is AFTER the start date. Since they are both set the same in the template, it won't let us submit until we change the end date, even by a minute or two. I've tried to use the gs date functions available to calculate a few minutes from now but it seems that I cannot use any calculated date in the template. It just doesn't work. Even using something like

gs.minutesAgo(5)


doesn't return anything when in the template.


So I guess my question is, is there a way to use a template to fill in 'today at midnight' in the start field and then today at 12:01 in the end field using a template? The team that uses these templates would like to click as little as possible, hence using the templates to fill in the date/time.

 

Thanks!

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Sam,



Try this for your start and end dates.



Start date = javascript: gs.endOfToday();


End date = javascript: var gdt = new GlideDateTime(gs.endOfToday()); gdt.addSeconds(1); gdt.getValue();



6cb8c57f53.png



In my instance that gives me the following results when applying the template:


8d89e18cad.png


View solution in original post

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Sam,



Try this for your start and end dates.



Start date = javascript: gs.endOfToday();


End date = javascript: var gdt = new GlideDateTime(gs.endOfToday()); gdt.addSeconds(1); gdt.getValue();



6cb8c57f53.png



In my instance that gives me the following results when applying the template:


8d89e18cad.png


Thanks Brad! That did work. Sorry I didn't reply quicker. I never got notified anyone actually replied to this and I thought it was just being ignored



thanks again!


b-rad, the End date style you proposed doesn't seem to work anymore, any suggestions?



Thanks,


James


So I know this is 3 years old (6 years from the start of the thread) but I came up against this today and found a solution that worked for me so here it is:

At some point when updating templates various system objects became unavailable to "javascript:" style calls within templates. However, base base javascript objects are still available and as long as we return a string in the correct format the dates will still get set on the template. 

So what is the correct format? '2020-07-15 14:00:00'

So how do we get something like this with Javascript? In my case I didn't care about the time (wanted everything to be at the same time of day for a maintenance window) but I did care about setting the date:

var d = new Date();

// Do various manipulations on the date to adjust for circumstances

// Ex: d.setDate(d.getDate() + (5 - d.getDay()); // Set to Friday

gs.print(d.getFullYear() + '-' + (+d.getMonth() + 1) + '-' + d.getDate() + ' 14:00:00'); // Could use Date methods to produce the time but have to pad with zeros in some cases

Putting that all together our template might look like this:

javascript: var d = new Date(); d.setDate(d.getDate() + (5 - d.getDay()); d.getFullYear() + '-' + (+d.getMonth() + 1) + '-' + d.getDate() + ' 14:00:00';