getDateFromFormat not defined Error in Service Portal.

Rosy
Kilo Contributor

I am getting getDateFromFormat not defined in Service Portal. I have two fields on my form start date and end date. We have also users in Europe where their date format is dd--mm--yyyy

So our requirement is start date should not allow past dates and users should only select a date at least two business days into the future.

And end date should not be less than the start date. Can someone help me with the code.

I have written the following code but i am getting getDateFromFormat not defined.

//this code is written on start date filed

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

var today = new Date();
//g_form.addInfoMessage("Today's date is" + today);

var ga = new GlideAjax('emeaDateRestrict');
ga.addParam('sysparm_name','dateRestrict');
ga.addParam('sysparm_date', g_form.getValue('date_start'));
ga.getXML(ajaxParse);

function ajaxParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//g_form.addInfoMessage(answer);//see the full JSON response
answer = JSON.parse(answer);

if((answer <86400 && answer>-86400) || answer==0){
g_form.clearValue('date_start');
g_form.addInfoMessage('Please select a valid date. Please select a date at least two business days into the future..');
}
else if(answer<-86400){
g_form.addInfoMessage('You may not select a previous date. Please select a date at least two business days into the future');
g_form.clearValue('date_start');
}
}
}

//glideajax

var  = Class.create();
emeaDateRestrict.prototype = Object.extendsObject(AbstractAjaxProcessor, {

dateRestrict: function(){


var today = new Date();
var date = this.getParameter('sysparm_date');
var string = "G'day World";
var json = new JSON();
string = string + date + today;
var diffSeconds = gs.dateDiff(today, date, true);
//diffSeconds.addLocalTime(-1);



return json.encode(diffSeconds);


},
type: 'dr'
});

 

/''''''''''''''''''''''''''''''''''''''''' end date script'''''''''''''''''''''''''''''''''''''while using this script i am getting comparedates not defined error 

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

var end = g_form.getValue("date_end");
var start = g_form.getValue("date_start");

if (start == "" || end == "") {
return;
}

var format = g_user_date_time_format;
var isEndBeforeStart = compareDates(start, format, end, format);

if (end < start) {
alert("end must be after start");
g_form.setValue('date_end', '');
}
}

// i tried different code on end date variable after searching in the community, now i am getting getdatefromformart not defined error

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

var end = g_form.getValue("date_end");
var start = g_form.getValue("date_start");
// skip if start or end is blank
if (start == "" || end == "") {
return;
}

// get user's date time display format

var isValidStart = getDateFromFormat(date_start, g_user_date_format);

var isValidEnd = getDateFromFormat(date_end, g_user_date_format);


if (end < start) {
alert("end must be after start");
g_form.setValue('u_end_date', '');
return 1;
}
else{
return 0;
}



}

 

12 REPLIES 12

You will find docs on their website.

https://momentjs.com/

Jace Benson
Mega Sage

So this came up in a chat I was having this week.

 

To use the g_user_date_format and g_user_date_time_format and getDateFromFormat, you would have to reinclude the JS file that declares these things.

 

To do that goto the widget where you get a failure message and add a new dependency to a new UI script where you copy/paste the code from https://hi.service-now.com/scripts/calendar.js.

 

Then all your old calls ought to wrok as described in other posts.

var reqDate = new Date(getDateFromFormat(newValue, g_user_date_time_format));//returns a date object you can test against for date/time variables/fields
var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));//returns a date object you can test against for date variables/fields

 

Good to know thank you for sharing the solution - it works!

Thank you so much! It resolved the issue by following the same steps you mentioned. 🙂

 

Cheers

Ramandeep

Hi,  i do not have https://hi.service-now.com/scripts/calendar.js. account, please help me with script and where should i modify the code?