Comments on: Schedule-based Date/Time Addition https://servicenowguru.com/business-rules-scripting/schedule-time-addition/ ServiceNow Consulting Scripting Administration Development Thu, 07 Mar 2024 15:15:56 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Sourabh Dhaygude https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9054 Fri, 28 Jul 2017 15:10:18 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9054 I always struggle with date time calculations and Mark is the guy who always helps me. Thanks Mark for all these posts!

Kind regards,
Sourabh

]]>
By: MG https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9053 Thu, 08 Oct 2015 14:59:31 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9053 In reply to Mark Stanger.

This unfortunately isn’t working – I have the same need as well. Not sure how to put in a negative duration (if that is even possible).

]]>
By: Amisha Parekh https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9052 Fri, 27 Feb 2015 15:48:29 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9052 Hi Mark,

I tried following code in background script.

var todaysDate = new GlideDateTime();
todaysDate.setDisplayValue(gs.nowDateTime());

gs.print(“todaysDate display value : ” + todaysDate);

My Timezone is US/Eastern but, this is still returning output in GMT format asour instance is in GMT format.

Could you please let me know if i am missing anything?

Thanks & Regards,
Amisha Parekh

]]>
By: Daryll https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9051 Thu, 08 May 2014 16:29:25 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9051 In reply to Ryan.

I had this issue, this is what I can up with…

// Date Time variable taken from my form
var dateTime = g_form.getValue(‘u_date_and_time’);
// a duration number (minutes in this case)
var duration = g_form.getIntValue(‘u_duration’);

// Split up the Date and Time for manipulation and reordering
var dateSplit = dateTime.split(” “)[0];
var timeSplit = dateTime.split(” “)[1];

var date = dateSplit.split(“-“)[0];
var month = dateSplit.split(“-“)[1];
var year = dateSplit.split(“-“)[2];

var hours = parseInt(timeSplit.split(“:”)[0]);
var minutes = timeSplit.split(“:”)[1];
var seconds = timeSplit.split(“:”)[2];

// Reorder & combine dateTime and create the endDateTime
dateTime = (year + “-” + month + “-” + date + ” ” + hours + “:” + minutes + “:” + seconds);
// Where I added minutes on…
endDateTime = (year + “-” + month + “-” + date + ” ” + hours + “:” + minutes + duration + “:” + seconds);

// This allows you full flexibility with regards to changing any parts of the time or date and changing the order

In your case you could just do…

date = date – 1;

You may want to check that date isn’t 1 and if it is reduce the month by 1 and work out which month it is for the days then set the date to the last day of the previous month.
Not an ideal solution but it should help.

]]>
By: Mark Stanger https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9050 Tue, 18 Feb 2014 16:31:04 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9050 In reply to David Martin Clavo.

I haven’t tried this specifically, but you might try ‘sched.subtract’ instead.

]]>
By: David Martin Clavo https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9049 Tue, 18 Feb 2014 16:22:05 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9049 Dear Mark,

Thanks for this article which has already helped me on several occasions.

Do you know of any way to perform a substract instead of an add?

I tried with:

var timeToAdd = -86400; // negative time
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, ”);

But sched.add just returns the same value as currentDateTime.

Use case: I have a task where I know the due date, its duration, and a schedule, and would like to calculate the start date accordingly.

Thanks in advance for your reply,
David

]]>
By: Hiranmayee https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9048 Mon, 13 Jan 2014 12:39:54 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9048 Thanks for all your help Mark

]]>
By: Mark Stanger https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9047 Mon, 13 Jan 2014 12:14:27 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9047 In reply to Hiranmayee.

Unless ServiceNow has it documented somewhere (and I don’t think they do in this case) I don’t know how you would know unless you had used it before. A close examination of my code above would have told you the data type of the information being passed in but I can see how that might be overlooked. At least now you’ll know for next time. I would contact ServiceNow and request that they document GlideSchedule so it’s available in the future.

]]>
By: Hiranmayee https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9046 Mon, 13 Jan 2014 11:08:37 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9046 Thanks Mark for your information.

It worked.

One question: Is there any way to check the method in a package because we faced issue because we didnt know what was the input parameter data type as well as output parameter.

Thanks in advance.

]]>
By: Mark Stanger https://servicenowguru.com/business-rules-scripting/schedule-time-addition/#comment-9045 Fri, 10 Jan 2014 14:50:53 +0000 https://servicenowguru.wpengine.com/?p=4076#comment-9045 In reply to Hiranmayee.

I’d be surprised if this worked in any release, let alone Calgary. The problem isn’t with the code I’ve supplied above, but you do have an issue with the date your passing from the client. The date is being passed as a string to your back-end function because that’s how the date values are formatted client-side. The ‘add’ method requires a ‘glideDateTime’ object, not a string. You’ll have to convert the string being passed into your function before using ‘sched.add’. Something like this should work better to replace the ‘var firstDT = this.getParameter(‘sysparm_fdt’); //First Date-Time Field’ line in your script above.

var firstDT = new GlideDateTime();
firstDT.setDisplayValue(this.getParameter(‘sysparm_fdt’));
]]>