Client Script: Return dates in same format (M/D/Y)?

Edwin Fuller
Tera Guru

Can someone show me how I can get two dates in the same format? Below is an onChange client script that runs on a date field. I need the oldValue to return in the format of M/D/Y just like the new value, see screen shot below.

find_real_file.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading || newValue === '') {

return;

}

alert("Old Value = " + oldValue + " New Value = " + newValue);

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

It appears that oldValue is the stored value of the field and not the display value that your selected date is. Since ServiceNow stores all of its dates in the format of YYYY-MM-DD, you should be able to take the date string from oldValue and split it into different variables to display your alert using the expected format MM-DD-YYYY


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


      if (isLoading || newValue === '') {


              return;


      }


      if (oldValue != '') {


              dateArray = oldValue.toString().split('-');


              dYear = dateArray[0];


              dMonth = dateArray[1];


              dDay = dateArray[2];


              oldFormated = dMonth + '-' + dDay + '-' + dYear;


              alert('Old Value: ' + oldFormated + ' New Value: ' + newValue);


      }


}


View solution in original post

7 REPLIES 7

nehas
Kilo Contributor

Can you try something like this




  1. if(requesteddate){  
  2.               var reqDateTime = requesteddate + ' 17:00:00';  
  3.               g_form.setValue('stnd_req_date',reqDateTime);  
  4.       }  

That doesn't give me the date format in M/D/Y


I think, user's profile dateformat and system dateformat should be same.



find_real_file.png


Gowrisankar Sat
Tera Guru

Check if this script helps:



function isValidDate(value){


      if(value == '' || value == undefined || value == null){


              return false;


      }


      return (getDateFromFormat(value, g_user_date_format) != 0);


}