getDateFromFormat not defined Error in Service Portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2018 07:55 AM
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;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2018 09:56 AM
I used below onChange script for end date validation. For Portal only.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
return;
// Date format
var dateFormat = g_user_date_format.toUpperCase();
// Make sure end date is valid
if (!(new moment(newValue, dateFormat).isValid())) {
alert('The date entered is not a valid fromat, please use the date selector or enter using the format: ' + dateFormat);
g_form.setValue('start_date', '');
g_form.setValue('end_date', '');
}
else {
// Variables used
var startStr = g_form.getValue('start_date');
var startDate = new moment(startStr);
// Check that end is not in the past
if (startDate.diff(g_form.getValue('end_date'), 'days') >= 0) {
// The end date cannot be the same or less than the start date
alert('The end date must occur after the start date.');
g_form.setValue('end_date', '');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2018 08:39 PM
Hi Mike,
I tried your code, now i am not getting that getDateFromFormat error in portal, but i am able to select even the past dates which should not happen.
The requirement i got was:
1) The start date should not be selected as the past date and should only select after 2 business days prior to the current date. Like for example if today is 3/21, user should only be able to start selecting from 3/23.........
2) I need the end date should be after start date.
I am implementing this in portal. Can you please help me with the code?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2018 08:42 PM
You may find solution here
https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2019 12:41 AM
Hi Mike
We found your post helpful for date validation.
I would like to know how can we use moment in future and it will be helpful if we get documentation about it.
Thank you.