Search code examples
datetimeservicenow

Add 10 days with selected date -on-change method -service now


I have 2 date fields issued date and due date.When I choose issued date,due date should be auto populated by adding 10days with selected date. I have written on-change method for this

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

    //var issuedDate=new GlideDateTime(g_form.getValue('u_issued_date'))
    //var issuedDate=g_form.getValue('u_issued_date')
    alert(issuedDate)
    var gdt = new GlideDateTime(issuedDate);
    gdt.addDays(10)
    g_form.setValue('u_due_date',gdt);
}

I am getting an error GlideDateTime is not defined function ().How can I achieve this? Is there any other way?


Solution

  • GlideDateTime is not available on client side. For simple operation like the one you are having you can use javascript Date object. Which is pain to format, but doable, example:

    var date = new Date(g_form.getValue('u_issued_date'));
    date.setDate(date.getDate() + 10);  //add 10 days
    g_form.setValue('u_due_date', formatDate(date));
    
    function formatDate (date) {
        return date.getFullYear() + '-' +
            leadingZero(date.getMonth() + 1) + '-' +
            leadingZero(date.getDate()) + ' ' +
            date.getHours() + ':' +
            date.getMinutes() + ':' +
            date.getSeconds();
    }
    
    function leadingZero (value) {
        return ("0" + value).slice(-2);
    }
    

    For more complicated operation you would wish GlideDateTime you will have to use GlideAjax, that will do operations on server side, and provide result.