Based on user selection date i need to calculcate days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2025 12:59 AM
HI,
i have start date (date/time) and end date (date/time) and Days as check boxs.
if user choose start date and end date and choose days like monday and wednesday means, it will calculate how many mondays & wednedays and set the value in number of days.
this is the script i written , but i am not able to move the logic
client script
--------------------
script include
--------------
Regards,
Rajesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2025 03:54 AM - edited 04-18-2025 03:56 AM
Hi @gillerlaraj
Try the below scripts.
Fields/Variables:
Script include:
var SJ_CatalogUtils = Class.create();
SJ_CatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDays: function() {
var startDate = new GlideDateTime(this.getParameter('start'));
var endDate = new GlideDateTime(this.getParameter('end'));
var count = {
'monday': 0,
'tuesday': 0,
'wednesday': 0,
'thursday': 0,
'friday': 0,
'saturday': 0,
'sunday': 0
};
while (startDate.compareTo(endDate) <= 0) {
var dayOfWeek = startDate.getDayOfWeek();
if (dayOfWeek === 1) count.monday++;
if (dayOfWeek === 2) count.tuesday++;
if (dayOfWeek === 3) count.wednesday++;
if (dayOfWeek === 4) count.thursday++;
if (dayOfWeek === 5) count.friday++;
if (dayOfWeek === 6) count.saturday++;
if (dayOfWeek === 7) count.sunday++;
startDate.addDays(1); // Move to the next day
}
gs.info("Monday: " + count.monday.toString());
gs.info("Tuesday: " + count.tuesday.toString());
gs.info("Wednesday: " + count.wednesday.toString());
gs.info("Thursday: " + count.thursday.toString());
gs.info("Friday: " + count.friday.toString());
gs.info("Saturday: " + count.saturday.toString());
gs.info("Sunday: " + count.sunday.toString());
return JSON.stringify(count);
},
type: 'SJ_CatalogUtils'
});
Client scripts (On change):
Note: You have to create 7 client scripts for each day (7 days). Script logic remains same. So just do insert & stay by selecting the day.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var mon = g_form.getValue('mon');
var tue = g_form.getValue('tue');
var wed = g_form.getValue('wed');
var thu = g_form.getValue('thu');
var fri = g_form.getValue('fri');
var sat = g_form.getValue('sat');
var sun = g_form.getValue('sun');
var total = 0;
var script = new GlideAjax('SJ_CatalogUtils');
script.addParam('sysparm_name', 'getDays');
script.addParam('start', g_form.getDisplayValue('start_date'));
script.addParam('end', g_form.getDisplayValue('end_date'));
script.getXMLAnswer(function(response) {
var count1 = JSON.parse(response);
if (mon == 'true')
total += parseInt(count1.monday);
if (tue == 'true')
total += parseInt(count1.tuesday);
if (wed == 'true')
total += parseInt(count1.wednesday);
if (thu == 'true')
total += parseInt(count1.thursday);
if (fri == 'true')
total += parseInt(count1.friday);
if (sat == 'true')
total += parseInt(count1.saturday);
if (sun == 'true')
total += parseInt(count1.sunday);
g_form.setValue('total_days', total.toString());
});
}
Result:
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2025 04:57 AM
@gillerlaraj Please try with below updated code
var daysCalculation = Class.create();
daysCalculation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
days: function () {
var sdate = this.getParameter('sysparm_sdate');
var edate = this.getParameter('sysparm_edate');
var selectedDays = {
1: this.getParameter('sysparm_mon') == 'true', // Monday
2: this.getParameter('sysparm_tue') == 'true', // Tuesday
3: this.getParameter('sysparm_wed') == 'true', // Wednesday
4: this.getParameter('sysparm_thurs') == 'true', // Thursday
5: this.getParameter('sysparm_fri') == 'true', // Friday
6: this.getParameter('sysparm_sat') == 'true', // Saturday
0: this.getParameter('sysparm_sun') == 'true' // Sunday (GlideDateTime considers Sunday as 0)
};
var count = 0;
var start = new GlideDateTime(sdate);
var end = new GlideDateTime(edate);
// Loop from start to end date
while (start.getDate().getNumericValue() <= end.getDate().getNumericValue()) {
var dayOfWeek = start.getDayOfWeekLocalTime(); // Returns 1=Monday to 7=Sunday
// Convert 7 (Sunday) to 0 to match JavaScript days
var jsDay = (dayOfWeek == 7) ? 0 : dayOfWeek;
if (selectedDays[jsDay]) {
count++;
}
// Move to next day
start.addDaysLocalTime(1);
}
return count.toString();
}
});
Please mark/correct helpful if this helps you!