ServiceNow Date Difference in Days Client Script | ServiceNow Date Comparison Implementation
hello friends welcome back to my youtube channel basico servicenow learning so in today's video we are going to implement servicenow date validation scenarios there are various service not date validation scenarios which we received while doing development right we receive lots of stories for date validation so in this video we are going to implement the same we are going to implement through client script by using simple javascript we are not going to use script include a glide sx because there are lots of ways people use those ways also to implement but in this video we are going to use a simple javascript to implement a various date validation scenarios so let's see what are all those scenarios which we are going to implement today so the first scenario is that we have to populate the current date in the start date field using client script so let me give you a example of that first here we have a personal development instance where we have two fields in incident record that can start date and the end date so when the page loads then this start date will get auto populated with the current date okay so this is required we are not going to populate it manually this will get auto populated okay now another scenario is and date should not be greater than the start date which says that user should not able to enter the end date which is smaller than the start date he can only enter the date let's say 11 12 13 because today is 10th of september and the third scenario is that user should only able to enter and date which comes between today and till coming 10 days for example today is 10th of september the user can only enter the date which comes between 10 till 20 means if user enter 21 then it will not allow to user enter the date if user enter nine then it will not allow user to enter the date user can only enter the there till 10 till 20 and vice versa let's say if today is 15 the user can only enter the date till 15 to 25 so let me give you example of the same so today is 10th of september so if user enter the date let's say 8 in end date then it will give him a alert message and not allowed to enter him that date if user let's say enter that 16 then 16 comes under range it means user can enter it and if user is entering let's say 30 then it will not allow him because it not comes under the range of 10 days okay so this is the scenarios which we are going to implement today and the last scenario is user should not able to enter the end date which is greater than today or less than the five days which says that today is 10 then user can only enter 9 eight seven six five okay he cannot enter two or one okay so these are two different date ranges which can be extended to like 90 days 60 days three four months five months okay 15 days 25 days depends on the only fact is that we have to only change the conditions while implementing the same so let's see how we can implement it in servicenow instance so let's navigate to our personal service service development instance so friend this is my servicenow personal development instance so i am going to implement these scenarios in incident record so earlier which i demonstrated that was a different instance so for demonstration perspective i have displayed the same so here in incident management or incident record we are going to implement this same scenario so here i have created two fields that is start date and end date so if you are using a personal development instance you might be not having these fields so create these two custom fields in your incident form so this start date field name is you underscore start date and this android field name is you underscore and date so in case if you are implementing for your application so what you have to do while doing a coding or while implementing the same logic you have to change the field name so we are going to implement the first scenario first that is get current date in start date field means when we load the page then this start date will get auto populated with the current date so let's start for a client script or let's write a client script type client script over here open this in a new tab or you can also click right click over here go to configure and go to client scripts okay that depends on you so just i'm navigating there i'm clicking on a new button so give some name let's say get current date or for better understanding let's say populate current date okay table is incident incident then we have to select the type so we want that on on load so i'm selling on load i've selected download now here i have to write the syntax or code so as i said earlier that we are going to use a plain javascript for the same so in javascript we have a way to get the current date that is where start javascript date okay so this is basically the object name which i am giving over here you can give any name over here so for understanding i have given the name with js which says whatever date we are getting over here the start date is from coming from the java script so this is the syntax which gives us start date or we can say current date so let me give you example first that how exactly the date look like because it is required okay so alert and save i'm not populating the date in start date when right now i'm just popping up the value so just reload the page again incident page consumer record once we reload see we have a alert message so this is the date which is given by the java script that's in text okay so this is the different format okay let me populate this format first over here so this is the format which we received okay and the format which we need to populate over here is see the date field let's say 10 so this is the format which we need to populate so these are two different format it is which says that we need to convert this particular javascript date to this format that is in servicenow format okay so this is required to do here so for that we have to write a code to change the format so let's see how we can change it because if we don't change it then this particular date in this format will not be acceptable over here right so let's see how we can populate so in java we had different functions to get those values let's say up to getting this javascript we had get full air that function we have to use for months we have to use get month and for date we can use get so let's see how we can use this so for understanding perspective this time i am giving the name date okay so because whatever date we are creating over here that is servicenow compatible right so that's why i have given the name start ascended here is javascript that's why i have given the date start gs date so here let's say new date dot get here okay so this full year gives us this here okay now i need this dash so let me give this dash over here now i need the month so the same way for getting the month that is new date dot get month okay so this will give me a month but here is a catch in in javascript when we get the values of month that comes between 0 to 11 okay if the month is december and if we are getting the value 11 over here then it will not be compatible with our requirement right so what we have to do we have to add one let's say if we are working for a january okay so january will give me a value through this code is zero okay but we required one so it will give me a one for this number it will return me a value that is 11 plus 1 12 so we are fulfilling our requirement okay so now the another thing is that here we have a 0 but we are only getting a value like 1 so 1 is not containing a zero in the string so what we going going to do here is that we'll also add a zero over here in a string okay now the another thing is that if we working on this month let's say 10 okay and then 0 then it will fetch me a value 0 1 0 okay so which is also not acceptable it means we have to slice the value which is coming from here means slice means let's say we have 0 1 0 then we can slice it to 0 1 so in javascript we have a slice method through which we can slice the values also okay so let me put in braces first these all the details okay dot and this is slice so let me slice it to minus 2 which says that from the right hand side it will take two values that is 0 1 and will slice all the values which comes after 2 so start from the right hand side to the left hand side in this way we are in the month now same we have to do for this date right the string so let's add the string now same we have to do for a month but for months we will not use plus one or something because in month javascript function will give me the same value let's say if today's 10 it will give me 10 if any other date let's say 15 then on 15 it will give me a 15 so we are not going to add one over there okay the same way basis but we need 0 because we have date from 1 to 9 where we need let's say today is 9 then we must be having 0 9 over here okay so the same we are getting over here plus 8. dot date okay so this is the way to get the date in javascript this function is used to get it okay that month okay date and get fully again we have to slice it for example if the date is 10 then it will come to 0 1 0 then it will be difficult for us right so we are slicing the same way minus 2. and here we have the syntax now the date which we have over here is servicenow dates let's see the alert of it save it let's do the page first the first is the date of the alert of javascript and we did not get the second date let's see why okay so let's see what we missed over here okay here we miss this basis okay sometimes it happens right by writing the code with miss something okay and here we put this in this okay now let's see here we miss something because whenever we are adding two strings or making in this way then sometimes it happens then we have the zero okay here we did it wrong we have to put this place over here because we are slashing the complete value we made braces over here it will not do it let's save it and let's see if it works let me load the page again so this is javascript date in that format and see now we have changed it to our required format right see 10 9 20 21 when the outcomes or we can say when the result comes what we require then it gives happiness okay so here we are done with this so let's remove this alert because we don't need that now what we need that we have to populate that field and in javascript service now we have a way to populate the field so the way is the underscore form dot set value we have to give field name over here so the field name is you underscore start date right as we have discussed earlier the tool there we have two fields that is start and end date and the name is underscore start date and you underscore and date so the value will be service not date start ascended and say so what happens when the page loads then it will populate this value in this field let's see save it the page see auto populated okay so in this way by using a simple javascript code we can populate the current date okay the only thing is that we should know that how to do that by using this syntax okay this is a very simple syntax well implementing the other scenarios we are going to use this also okay now the another scenario which we are going to implement that is and date should not be greater than the start date okay so here we are talking about two fields the start date and the end date start date is auto populated right now we have to apply a validation on on end date it means we have to write the on change client script means when user is entering the end date then it will give a validation to him in case if he is putting up a wrong date so let's get another client script click new let me give and validation as a name here i am giving a table name now we want to execute that on change on what field on end date okay so here we have a section where we can write the code so let's come over the page see here we already have a format if we are selecting the end date which says let's say 10 this is also in the servicenow format which says we don't need to uh arrange it in a required format as we did in here okay so this is a very simple way i believe you guys know that okay just for understanding i am just repeating it again that i am saying g underscore form dot get value whatever value we have in the start date field started okay let me populate it in a variable that is in start sn and i am just fetching the value from that field and underscore okay now what we have to do we only have to write a condition over there let's say if start and date is greater than okay then it will not allow user to do that or start let me copy the same let me put this here so the another condition is that we cannot fill the same date as start date so in case if start date is equal to ended then populate the validation message to user or give him an alert or if start date is greater than the end date then give a alert to him okay let me complete this basis first message into it the message should be do not enter the date right so message can be and it should be and here we can say after giving the alert that we have to normalize this value that is set value that is end date you underscore and underscore date to plan this is required right otherwise what is the need of validation then if we are not making changes on the field so here we are done with the code we are done with the condition let's save it so let's go to incident form reload it again leave so once we reload it again start it will get auto populated as we have written the code earlier on load let's say end date see if i select eight and it should be greater than the start date if i select the current date it is 10 and it should be greater than of a start date if i select the date which is greater than 10 then i am saying 12. see it allows me to enter this date so in this way we can apply a simple validation for start date and the end date now the another scenario is that user should only able to enter and date which comes between 10 to 20 because today is 10 okay then 20. so we have to up now write a code which will wait till 10 days so this is a specific scenario which we get most of the times like put a validation of 90 days put a validation of 60 days put a validation of a month okay so this is a required scenario let's see how we can implement the same in servicenow using javascript so let's navigate to servicenow instance so this is the same the on change what we are going to do we already did this scenario so just am deleting it okay i'm writing a fresh code because the same we are going to implement it on end date that user will not able to enter day 20 to 30 or the past date like seven okay so here let's start coding so let's see how we can implement it so the two thing here is that we have to fetch the value then we have whatever value we are going to fetch from the end date here is in servicenow format right then we have to change that servicenow format into javascript format okay earlier what we did we changed the javascript format into service now now here what we are going to do because user is manually putting up the value right so here we already having this format so what we are going to do we will change this format to javascript format okay then we calculate the time difference then we calculate a day difference in javascript and then we apply the condition so let's see how we can achieve it so first as i said earlier that first we have to get the value of the end date so i am in this way we can get it we all know that and s equals to okay so when users select the value then this says whatever value you just select will store in this variable okay now we have to calculate the date with a current date let's say user is entering a date which is less than that then it will apply a validation when user is entering the date which is less than or greater than the two-day date it means we need a two-day data as well so here for a fresh code i'm just again writing the code same we did for the start date okay so here i'm saying where start javascript date okay equals to new date so this means whatever started or i can say another word today date okay so today date is in javascript format and end date whatever we are getting in a servicenow format okay now what we have to do we have to first convert this date in javascript format okay so let's see how we can convert it where and js equals to so let's uh so to convert here is in javascript format that is new date and whatever variable we are getting over here put it here like this okay so let me put up the elect message to display you in response this is javascript format so let's again reload the page and let's say and date i am selecting 10 see because we have written on change so once we change the android value see the algorithm it will be giving me with the servicenow format now it is giving me a javascript format okay so see this is a syntax to convert a java a servicenow date into a javascript date so now we have two date that is today and the date which is selected by user okay now we have to find out the difference that is day difference that the day is four five six what is the difference between the selected date and from the today onwards so for that what we have to do in service now let's say y time difference equals to okay so how we can calculate a time difference the time difference which we calculating actually in milliseconds first we calculate the time difference in milliseconds then we convert those milliseconds into days in javascript okay so we have this ngstate for calculating perspective so the conversion is done because we are using a javascript so we cannot use servicenow things into it right by doing calculations because it will only identify javascript things right so here we have used this date which is coming from the javascript then we am saying get time so this function will give us a millisecond time and calculating i believe from 1970 there is a specific date in javascript from where the time gets calculated because we need at least a point from where we can say okay we are calculating the date right so here now this and it minus the today javascript data i can say so here i am saying so this is get time okay so what it did whatever date we are going to select here it will calculate the time difference whatever time is user selecting in the end date will manage the to the date time and give us the millisecond the difference millisecond let me give you a demo of it first time difference save it so once we save it let's reload the page this one sorry some morning time so sometimes you will be listening like shutter is opening or shutting is down people are going here and there okay so let's select the android 16 so see once i select the 16 it calculated the time in a milliseconds okay now we have a time in milliseconds so we have to convert that time into right into days so we how we can convert in day difference equals to we have to use this time difference and should divide this time difference with some values you have to learn these values okay 60 into 60 and 2 000 so we are 24 is basically r60 is mini 60 second and thousand is required to do this okay now we have written the formula so let's see the date difference to check it i'm just again doing it and doing i believe this is not annoying you because i want to give you a complete understanding of a code that why it is working like this or why we are doing like this so then if you practice or you will write then you can write it easily in your future at least you will know that what exactly you want to do because logic can be any the days can be any right if you understand the logic you can implement anything right so the difference here just save it save it now let's say reload the page again i am changing the condition over here to 16. see it is giving me a 5.92 because it is calculating from 1970 to a specific time right so whatever time currently we have he converted those milliseconds and two days so it means it is approx 6 days today is 10 and we have selected 16. okay let's say if we are selecting negative date let's say six so it will give me a minus four days so whenever we are writing a code for this we will utilize that okay so the logic is same for both means for this and this what we have to do we only have to change the conditions okay now we are done with the days also so let's remove it let's allow the conditions so condition can be the first condition is while doing that let's see if the selected end date is less than the current date then it will give us an error message and if the slack date is greater than the time being then it will give us a error message so let's implement the first condition that is today gs date and ngs date so we are not going to use this date because these are javascript date we are going to use this servicenow dates over here okay but we don't have a current service now date right so what we have to do we have to convert current or today's service in gs date into a service now date so how we can do it the same way which we did here right same way so let's move here and convert it first let's say variable today okay it was javascript now we are converting into ascendant equals to copy this as we already discussed about it okay so copy this so this the date which we are going to compare is in servicenow format the date which we are going to compare we already have in servicenow format so we are going to use this variable and this variable so i am saying if today s and date copy this is greater than and ascend date then it will give us our error message right so what will be that alert message let's say please select date ranges between today and upcoming days okay so whatever i understand i write this text whatever you use feel better you can write that text okay now again what we have to do we have to make the alert after giving a let me we should make the field this empty right so here again underscore dot set value and here select this field here okay so this is the first condition now the another condition is this is basically for if somebody select the date which is less than today now another condition is the date range so we have to use this day difference here let's see how we can write the condition for it let's say if this and ascend date is greater than this today ascend date the first condition is this then what we have to do we have to check means end that day difference this day copy this this is greater than 10 at my point let me give you example of the same first let me put the braces because mostly i missed these basis like i did earlier and complete this and paste it which says if user select the date which is greater than the today's date and if the date difference the calculated date difference is more than 10 then do not allow user okay so which automatically says if the date difference is less than 10 it allows the user to enter the date okay let's save it let's load the page again let's see first condition select the last date no it's not allowing me to do that if i select 10 see it allows me to do that if i select 16 it allows me to do that if i select 29 the date ranges is like 10 to 29 is 19 days it is not allowing me to do that okay so we have implemented the third condition or third scenarios as well now the fourth is this where we have to lessen today till five days so what we have to do till here the code will be same only you have to change the conditions so guys this is a task for you change the conditions apply for yourself in case if you feel any problem then do comment us even though we have already posted all the code which we have written over here in our website that is basic or servicenow learning dot n i will also share the link below where you can get the code and you can try all the things by yourself so guys if you are looking for any solution or any service no validation logic and if this video answers your question then please do share and subscribe okay and please like our video if you find that this video is helpful for you okay friends so this is all for this video thank you bye bye take care have a nice day
https://www.youtube.com/watch?v=RvdEbLt799M