Add days to Date/Time field and show on Service Portal

Shrey hurana
Tera Contributor

Hello All,

I have a requirement where I have 2 fields i.e. 'Please Select the duration' (Radio Button with 2 fields i.e. 24 Hours and 48 Hours)(Name - please_select_the_duration) and 'Start Date' (Date Time field) (Name - start_date). Based upon the radio button selected and on change of Start Date the End date field needs to be populated by adding 24 Hours or 48 Hours based upon the selectiom.

I tried multiple approaches from community, but no luck. Can someone please help me with the same. 

 

 

 

Script Include:

pastDate: function() {
         var selected_date = new GlideDate();
         selected_date.setDisplayValue(this.getParameter('sysparm_Date'));
         var current = new GlideDate();
         var start_check = gs.dateDiff(current, selected_date, true);

        var radio_button = this.getParameter('sysparm_Duration');
        var start_date = this.getParameter('sysparm_Date');


        if (start_check > 0 || selected_date >= current)
            //return 0;
            {
                return 'SHREY';
            }
            
        else if (start_check < 0)
            return 2;
    },
 
Client Script (Written on change of Start Date field):
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    

    var radio_button = g_form.getValue('please_select_the_duration');
var ga = new GlideAjax('UtilsAjax');
        ga.addParam('sysparm_name''pastDate');
        ga.addParam('sysparm_Date', newValue);
        ga.addParam('sysparm_Duration',radio_button);
        ga.getXML(CheckDateValidation);
    }

function CheckDateValidation(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer"); 
    
    if (answer == 2
        {
            g_form.setValue('start_date''');
        g_form.showFieldMsg('start_date''Please select the future date','error');
        }
        else 
{
    alert('IMPLEMENTATION IN PROGRESS');
    
}

}
 
 
1 ACCEPTED SOLUTION

So in the script include you write:

var dt = new GlideDateTime(start_date);

This will only work if start_date is a string that contains a date and time in UTC time zone and format YYYY-DD-MM HH:mm:ss.

The original poster wrote:

var selected_date = new GlideDate();
selected_date.setDisplayValue(this.getParameter('sysparm_Date'));

which is way better, only it is the wrong object.

Should have been:

var selected_date = new GlideDateTime();
selected_date.setDisplayValue(this.getParameter('sysparm_Date'));

That is because GlideDateTime's setDisplayValue expects a string representing a date and time in the current user's time zone and the current user's format - whichever that format is.

View solution in original post

22 REPLIES 22

-O-
Kilo Patron
Kilo Patron

If the input and output is date/time, you need to use GlideDateTime in your code.

But what is the requirement here actually?

You talk about computing a date/time offset by 1 or two days, but your code seems to be about validating something?

Can you clarify?

Shrey hurana
Tera Contributor

I have 2 requirements i.e. start date should not be before the current date -> This is working fine. (As my script includes return 2 in that case and shows the field message). This is done and working as expected.

 

Second requirement is that i need to populate the end date field based upon the duration amd start date selected. If 24 hours selected then it should populate the end date by adding 24 hours to the the start date amd same case for 48 hours.

Vishal Birajdar
Giga Sage

Hi @Shrey hurana 

 

If my understanding is correct you want to set end date depending upon start date and duration selected on form...??

Try below code.

 

Script Include:

pastDate: function() {
      // object to return  data  
    var result ={
          'isStartDateValid':'',
          'endDate':''
}
// get values selected on form
        var radio_button = this.getParameter('sysparm_Duration');
        var start_date = this.getParameter('sysparm_Date');
 
//Check if start date is not past date
var getCurrentDate = gs.nowDateTime();
 
if(start_date > getCurrentDate) {
result.isStartDateValid = true;
} else {
result.isStartDateValid = false;
}
// get future date depends upon duration selected.
var dt = new GlideDateTime(start_date);
 
if (radio_button == '24 hours'){ //use your backend value here
dt.addDaysUTC(1);   //24 hrs = 1 day
} else {
dt.addDaysUTC(2); //48 hr = 2 days
}
result.endDate = dt;
 
return JSON.stringify(result);
 
Client Script (Written on change of Start Date field):
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    

    var radio_button = g_form.getValue('please_select_the_duration');
var ga = new GlideAjax('UtilsAjax');
        ga.addParam('sysparm_name''pastDate');
        ga.addParam('sysparm_Date', newValue);
        ga.addParam('sysparm_Duration',radio_button);
        ga.getXML(CheckDateValidation);
    }

function CheckDateValidation(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer"); 
var result= JSON.parse(answer);
  if (result.isStartDateValid == true
        {
//Set the end date depending upon result returned by script include 
            g_form.setValue('end_date',result.endDate);
       
        } else {
alert("start date should not be past date");
}
 
       }
 
}
Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

I have tried the same code earlier also but no luck.

I am getting the error message at End date field. Can you please let me know how to fix it.