How to convert date format to dd-mm-yyyy to yyyy-mm-dd in catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 07:37 PM
Hi,
I have a scenario to, check the selected date(field name- date_to_check) with target start and end date.
user selects a date(field name- date_to_check) in service portal,
So, when i retrieve that in catalog client script it shows as dd-mm-yyyy format.
Then I retrieve two other dates(start date and end date) from a table project allocations table. It shows in yyyy-mm-dd format.
- start_date - yyyy-mm-dd format
- end_date - yyyy-mm-dd format
- date_to_check - dd-mm-yyyy format
I want to check below condition with these three date values.
if ((date_to_check <= end_date && date_to_check >= start_date )) {
}
this condition cannot be checked since dates are in different format.
I have tried several methods to do this conversion but none of them worked. I have used
date_to_check .getByFormat('yyyy-MM-dd')
this returns undefined.
Please anyone could guide me how to achieve this. It will be a great help.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 10:09 PM
There are going to be different ways to do it, however I have done this by using a GlideAjax call. Here is an example of the Client Script (you would simply call the start_expire_compare function):
function start_expire_compare() {
if (g_form.getValue("u_expiration_date") != '' && newValue != '') {
// Check if start date is before expiration date
var ga_compare = new GlideAjax("Account_Management");
ga_compare.addParam("sysparm_name","check_date1_before_date2");
ga_compare.addParam("sysparm_date1", newValue);
ga_compare.addParam("sysparm_date2", g_form.getValue("u_expiration_date"));
ga_compare.getXML(callback_compare_start);
}
return;
}
function callback_compare_start(response) {
var compare_date = response.responseXML.documentElement.getAttribute("answer");
if (compare_date == 1) {
g_scratchpad.start_date = false;
alert ("You have entered a start date of " + newValue + ". The start date cannot be after the expiration date.");
g_form.showErrorBox("u_start_date","The start date cannot be after the expiration date.");
g_form.setValue("u_start_date","");
}
else {
g_scratchpad.start_date = true;
}
return;
}
This is what the Script Include looks like:
check_date1_before_date2: function() {
var date1 = this.getParameter('sysparm_date1');
var date2 = this.getParameter('sysparm_date2');
var date1_gdt = new GlideDateTime(date1);
var date2_gdt = new GlideDateTime(date2);
// Return: 0 = Equal, 1 = date1 after date2, -1 = date 1 before date 2
return date1_gdt.compareTo(date2_gdt);
},
In the Client Script, I use the scratchpad variable as a way to prevent the user from proceeding until the GlideAjax call is complete. It doesn't take long, but someone moving really quickly could submit before it returns. Just wanted to explain what that was, as it may seem a bit out of place.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 10:31 PM
Hi,
You can use the below piece of code to format the date to yyyy-mm-dd.
var date= new GlideDate();//replace new GlideDate() with the incoming date
var date2 = date.getByFormat('yyyy/MM/dd');//now use the date2 to set the field value
Kindly mark my answer as Correct and Helpful based on the Impact.
Regards,
Gunjan
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2022 09:19 PM
Hi,
Try below code
Script Include:
var dateTimeValidation = Class.create();
dateTimeValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
dateValidate: function() {
var dateToCheck = this.getParameter('sysparm_date');
var startDate = this.getParameter('sysparm_start_date');
var endDate = this.getParameter('sysparm_end_date');
dateToCheck = new GlideDateTime(dateToCheck);
dateToCheck = dateToCheck.getDate();
startDate = new GlideDateTime(startDate);
startDate = startDate.getDate();
endDate = new GlideDateTime(endDate);
endDate = endDate.getDate();
var obj = {};
if (dateToCheck.before(startDate)) {
obj = {
"msg": "date cannot be before start date",
"isTrue": true
};
} else if (dateToCheck.after(endDate)) {
obj = {
"msg": "date cannot be after end date",
"isTrue": true
};
} else if (dateToCheck.equals(startDate) || dateToCheck.equals(endDate)) {
obj = {
"msg": "date cannot be same as start date or end date",
"isTrue": true
};
}
return JSON.stringify(obj);
},
type: 'dateTimeValidationAPR'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('dateTimeValidation');
ga.addParam('sysparm_name','dateValidate');
ga.addParam('sysparm_date',newValue);
ga.addParam('sysparm_start_date',g_form.getValue('start_date');
ga.addParam('sysparm_end_date',g_form.getValue('end_date');
ga.getXMLAnswer(callback);
function callback(response){
var ans = JSON.parse(response);
if(ans.isTrue == true){
g_form.clearValue('date_to_check');
g_form.showErrorBox('date_to_check', ans.msg);
}
}
}
Mark my answer correct & Helpful, if Applicable.
Thanks