ServiceNow Date Difference Scenario's and Code Solutions

While doing ServiceNow development we received lots of stories related to ServiceNow date difference, get current date in ServiceNow, compare dates in ServiceNow and many other date related stories etc. There are lots of ways to implement the date related requirement in ServiceNow. In this article we have shared various ServiceNow date validation scenario's through which you can calculate ServiceNow date difference in client script, you can find ServiceNow today's date, you change ServiceNow date format, you can subtract date in ServiceNow, conversion into servicenow date format yyyy-mm-dd and you can also calculate ServiceNow date difference in days. Here in this article we will share the code, which will help you to develop or implement the date related requirement in your ServiceNow instance. 
By doing minimal modification in below codes, you will get the exact output which you need for your ServiceNow date related requirement. All the code mentioned below is only for client-side validation. Almost all the ServiceNow date related scenarios we have covered as mentioned in table of content below.

Table of Content:
  1. ServiceNow get current date (ServiceNow Today's Date)
  2. ServiceNow get current year
  3. End Date should not greater than start date in ServiceNow (ServiceNow Date Comparison)
  4. Date should not greater than of 10 days from the current Date (ServiceNow Date Difference Scenario)
  5. Date should not smaller than of 5 days From Current Date (ServiceNow Date Difference Scenario)
  6. Java Script Functions we can use for Date Related Requirement
  7. Video Demonstration of implementing above topics in ServiceNow 
servicenow date validation client script

ServiceNow Get Current Date (ServiceNow Today's Date):

Below is the syntax you can directly use in your onload client script to get the current date in servicenow:

var startDate=new Date().getFullYear()+'-'+("0"+(new Date().getMonth()+1)).slice(-2)+'-'+("0"+new Date().getDate()).slice(-2);
g_form.setValue('your field name',startDate);

ServiceNow Get Current Year

Below is the syntax through which you can directly get the current year

var currentYear=new Date().getFullYear();
alert(currentYear);

Date should not greater than of 10 days from the current Date (ServiceNow Date Difference Scenario):

Below is the code, which we have implemented on onChange() client script. Where user can only select the dates in-between today and till the 10 days from today. Means user can select date in the range between today to till 10 future days.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

// Below code will return date in '2021-09-08' format (ServiceNow Date Format)
   var startDate=g_form.getValue('u_start_date'); 
//Below is only for understanding and below code will return date in 'Wed Sep 08 2021 08:56:21 GMT+0530 (India Standard Time)-(Java script date format)' you can uncomment same and see the result in alert box.
//    var javaScriptDate=new Date();
//    alert (javaScriptDate);
//Acutally we need javascript date in ServiceNow date format means like '2021-09-08' so for that we have used below syntax
var currentDate=new Date().getFullYear()+'-'+("0"+(new Date().getMonth()+1)).slice(-2)+'-'+("0"+new Date().getDate()).slice(-2);
alert(startDate);
alert(currentDate);
//Through this actually we are converting '2021-09-08' to 'Wed Sep 08 2021 08:56:21 GMT+0530 (India Standard Time)'
var stDate=new Date(startDate);
var curDate=new Date(currentDate);
// The time difference is actually calculated in milliseconds. The getTime() method returns the number of milliseconds and the internal clock in JavaScript starts at midnight January 1, 1970, so the calculation of milliseconds for a date is calcualted from  midnight January 1, 1970 to current date selected in the field.
var timeDifference=stDate.getTime()-curDate.getTime();
alert(timeDifference);
// The below formula will convert the milliseconds in Days.
var dayDifference = timeDifference / (1000 * 60 * 60 * 24);

if(startDate<currentDate)
{
alert("You can only select date from today’s till 10 days from today");
g_form.setValue('u_start_date','');
}
if((startDate>currentDate) && (dayDifference>10))
{
alert("You can only select date from today’s till 10 days from today");
g_form.setValue('u_start_date','');
}

   
}

Date should not smaller than of 5 days From Current Date (ServiceNow Date Difference Scenario):

Let's assume the ServiceNow date requirement is where user can only give past date in the field but the restriction is that he can only enter the date till last 5 days not smaller than that and even he cannot enter the current date. Then you can use below code. You can modify the code as per your requirement lets says for 90 days, 30 days etc. The only thing you have to do is that you have to modify the conditions:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

// Below syntax will return date in '2021-09-08' format (ServiceNow Date Format)
   var startDate=g_form.getValue('u_start_date'); 
//Below is only for understanding and below code will return date in 'Wed Sep 08 2021 08:56:21 GMT+0530 (India Standard Time)-(Java script date format)' you can uncomment same and see the result in alert box.
//    var javaScriptDate=new Date();
//    alert (javaScriptDate);
//Acutally we need javascript date in ServiceNow date format means like '2021-09-08' so for that we have used below syntax
var currentDate=new Date().getFullYear()+'-'+("0"+(new Date().getMonth()+1)).slice(-2)+'-'+("0"+new Date().getDate()).slice(-2);
alert(startDate);
alert(currentDate);
//Through this actually we are converting '2021-09-08' to 'Wed Sep 08 2021 08:56:21 GMT+0530 (India Standard Time)'
var stDate=new Date(startDate);
var curDate=new Date(currentDate);
// The time difference is actually calculated in milliseconds. The getTime() method returns the number of milliseconds and the internal clock in JavaScript starts at midnight January 1, 1970, so the calculation of milliseconds for a date is calcualted from  midnight January 1, 1970 to current date selected in the field.
var timeDifference=stDate.getTime()-curDate.getTime();
alert(timeDifference);
// The below formula will convert the milliseconds in Days.
var dayDifference = timeDifference / (1000 * 60 * 60 * 24);

// You can change the conditions as per your requirement
if(startDate>currentDate)
{
alert("You have to enter past date till 5 days from today");
g_form.setValue('u_start_date','');
}
if((startDate<currentDate) && (dayDifference<(-5)))
{
alert("You have to enter past date till 5 days from today");
g_form.setValue('u_start_date','');
}

// If you want that user can enter current date in servicenow form than you can remove the below if condition
if(dayDifference==0)
{
alert("You have to enter past date till 5 days from today");
g_form.setValue('u_start_date','');
}
}

End Date should not greater than start date in ServiceNow (ServiceNow Date Comparison):

Below is the code to compare the dates in servicenow. Date comparision is very simple. Mostly the date comparison is done between two dates for e.g., User should not select date greater than start date etc.
Below in onchange client script on field end date. means when user change the end date than this code will run. you can change your field name in place of u_start_date and u_end_date as per your requirement.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var startDate=g_form.getValue('u_start_date');
var endDate=g_form.getValue('u_end_date');
if(startDate>endDate)
{
alert("Please select date greater than of Start Date");
g_form.setValue('u_end_date','');
}
}

Java Script Functions we can use for Date Related Requirement:

As you have already checked above code examples. Below are the very basic java script functions we can use while working on ServiceNow dates requirements.

  • getDate();   //The getDate() method returns the day of the specific month (from 1 to 31)
  • getFullYear();  //getFullYear() method returns the year. for e.g., 2021. 
  • getMonth();   // The getMonth() method returns the month (from 0 to 11). Means January is 0 and December is 11
  • var currentDate=new Date();  //JavaScript uses the browser's time zone and display a date as a full text string for e.g. '2021-09-08' to 'Wed Sep 08 2021 08:56:21 GMT+0530 (India Standard Time)'

Video Demonstration of implementing above topics in ServiceNow

We have used above code in ServiceNow date difference scenarios. Below is the video demonstration of the same which will provide you complete understanding of almost all the date related scenario's in ServiceNow.


We believe all the above content related to ServiceNow date comparison, ServiceNow date format, ServiceNow date difference in client script, date functions, finding ServiceNow today's date, converting into servicenow date format yyyy-mm-dd and other date related scenario's will help you to understand the required date concepts in depth and help you to implement the logic in your ServiceNow instances. Please provide your feedback below and if this article is helpful to you then please do let us know. 

No comments:

Thankyou !!!!

Powered by Blogger.