
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 11:17 AM
Hey there SN Comm,
Looking for some help with a client script please.
Requirement:
Date field called 'Deadline' - When entered/changed, I need another field called 'Timing', to display certain choices:
If Deadline = today (timing = "overdue")
If Deadline = in the future but less than 180 days from current system date, (timing = “Deadline Approaching”)
If Deadline = in the future but <= 12/31 of the current year, (timing = “This Year”)
If Deadline = in the future but > 12/31 of the current year, (timing = “Future”)
I tried creating a test client script so far to get me going, but I get Timing = "Undefined" in the field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var theDeadline = g_form.getValue('u_deadline');
var theTiming;
var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));
var today = new Date();
if (today == theDeadline) {
theTiming = "Overdue";
}
g_form.setValue('u_timing', theTiming);
}
Anyone able to help?
John Feist - want to take another stab at more client scripts with me? LOL
Cheers!
-Rob
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2019 12:12 PM
I was able to figure out what I needed in order to make this work how I now want it too.
Adding my code in here in case it helps anyone. Making this as correct to close the post.
Thanks all!
-Rob
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var theTiming;
var today = new Date();
//get the entered date string
var dtstring = g_form.getValue('u_deadline');
//convert entered date to Date object
var dateToCheck = new Date(dtstring);
var approaching = new Date();
approaching.setDate(today.getDate() + 179);
//Compare the two numbers
if (dateToCheck < today || dateToCheck == today) {
theTiming = "Overdue";
g_form.setValue('u_timing', theTiming);
}
else if (dateToCheck <= approaching) {
theTiming = "Within 6 Months";
g_form.setValue('u_timing', theTiming);
}
else if (dateToCheck > approaching) {
theTiming = "6+ Months";
g_form.setValue('u_timing', theTiming);
}
else {
theTiming = "Where did that Date come from?";
g_form.setValue('u_timing, theTiming');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 11:32 AM
You would have to convert var today = new Date(); and do your Math
https://www.servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/
Please mark my response as correct and helpful if it helped solved your question.
-Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2019 08:44 AM
Thanks for the reply Prateek!
Now, I just need to know how to do that actual math part (which I am not a full on coder yet) - so this is going to require some research.
Cheers!
-Rob

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 08:18 AM
Is anyone able to help me configure script to do the following:
If Deadline (value form) is in the future but <= 12/31 of the current year, “This Year”
If Deadline (value form) is in the future but > 12/31 of the current year, “Future”
I am having a tough time figuring this out in order to script correctly.
Thank you!
-Rob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 11:30 AM
//Try this.partially tested on PDI. needs more testing
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
if (isLoading || newValue === '') {
return;
}
var theTiming;
var theDeadline = new Date(getDateFromFormat(g_form.getValue('u_deadline'), g_user_date_time_format));
var reqDate = new Date(getDateFromFormat(newValue, g_user_date_time_format));
var today = new Date();
var yearEnd = new Date(new Date().getFullYear(), 11, 31);
if (Number(theDeadline) == Number(today)) {
theTiming = "Overdue";
}
if (Number(theDeadline) > Number(today)) {
theTiming = "Future date";
if(Number(theDeadline) < Number(yearEnd))
{
theTiming = "This year";
}
else
{
theTiming = "Future";
}
}
if (Number(theDeadline) < Number(today)) {
theTiming = "Back date";
}
console.log("theTiming = " + theTiming);
}
Vinod Kumar Kachineni
Community Rising Star 2022