
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2023 09:24 AM
Hello all,
I am working on a script to return all the given mondays between two provided dates. I have been dabbling with the below script I came accross but it gives me the mondays after the end date. I am not understanding it correctly. It should print out 01/23, 01/30, 02/06, and 02/13 but it is not doing it. Is it because I am not using GlideDateTime()?
function getDaysBetweenDates(start, end, dayName) {
var result = [];
var days = {sun:0,mon:1,tue:2,wed:3,thu:4,fri:5,sat:6};
var day = days[dayName.toLowerCase().substr(0,3)];
var current = new Date(start);
// Shift to next of required days
current.setDate(current.getDate() + (day - current.getDay() + 7) % 7);
// While less than end date, add dates to result array
while (current < end) {
result.push(new Date(+current));
current.setDate(current.getDate() + 7);
}
return result;
}
// Get Mondays between these dates
gs.print(getDaysBetweenDates(new Date(2023,01,17),new Date(2023,02,17),'Mon'));
Any help is appreciated!
thank you,
Y
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2023 09:40 AM
// Set the start and end dates
var start = new GlideDateTime("2023-01-17");
var end = new GlideDateTime("2023-02-17");
// Create an empty array to store the Mondays
var mondays = [];
// Loop through the dates
for (var date = start; date.compareTo(end) <= 0; date.addDays(1)) {
// Check if the current date is a Monday
if (date.getDayOfWeek() == 1) {
// Add the date to the array of Mondays
mondays.push(date.getDisplayValue());
}
}
// Return the array of Mondays
return mondays;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2023 09:40 AM
// Set the start and end dates
var start = new GlideDateTime("2023-01-17");
var end = new GlideDateTime("2023-02-17");
// Create an empty array to store the Mondays
var mondays = [];
// Loop through the dates
for (var date = start; date.compareTo(end) <= 0; date.addDays(1)) {
// Check if the current date is a Monday
if (date.getDayOfWeek() == 1) {
// Add the date to the array of Mondays
mondays.push(date.getDisplayValue());
}
}
// Return the array of Mondays
return mondays;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2023 12:58 PM
Thank you Rajeev! I was able to get it working with your code. Thank you for your help!