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

Issue is that I have multiple users and they are using different date format based upon there preference and country location. I need to design a script which works based upon the date preference of the user and for all the date preference. Can you please help me how I can design this.

@Vishal Birajdar  

Hint: the problem is with the input, not the output - or rather how you take in the start date.

Hint2: the original poster does it way better, only using the wrong object.

Hi @-O- 

 

Indeed...!!

Date time format will be taken from input and its working absolutely fine for me.

For @Shrey hurana its not working.

 

 

Vishal Birajdar
ServiceNow Developer

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

You mean you have changed the date/time format of your user profile to MMM DD,YYYY HH:MM:SS and it worked. Than changed it to dd/MM/yy HH:mm:ss and it still worked?

I mean the idea here is the solution to work under any condition, not just for you.

This test case I have not checked.

Let me check.

Vishal Birajdar
ServiceNow Developer

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