I'm trying to find a solution to get date (yyyy-MM-dd) from theses parameters for example :
- year : 2021
- month : 2 (february)
- week : 1 (1st week of february)
- day : 1 (monday)
Do you have some ideas ?
Thanks :)
I'm trying to find a solution to get date (yyyy-MM-dd) from theses parameters for example :
Do you have some ideas ?
Thanks :)
I've done this, it's not pretty but it works
//Parameters
var year = '2021',
month = '11',
week = '4',
day = '5';
gs.log(getDateForFloatingBusyDay(year, month, week, day));
function getDateForFloatingBusyDay(year, month, week, day) {
if (month.length < 2) {
month = '0' + month;
}
var arr_date = generateDaysForMonth(year, month);
//Loop on generated date to match the first date that match the week number
for (var x in arr_date) {
var d = new Date(arr_date[x]);
//if week number from input date match looking week number go trought
if (getWeekOfMonth(d, true) == week) {
var firstDayOfWeek = formatDateForSnow(arr_date[x]);
//Monday = 2 in SNOW as we start on weeks on monday we substract 2
day = parseInt(day) - 2;
//add days to first day of week
var gdt = new GlideDateTime(firstDayOfWeek);
gdt.addDaysUTC(day);
return gdt.getDate();
}
}
}
//Get week number for a date
function getWeekOfMonth(date, exact) {
var month = date.getMonth(),
year = date.getFullYear(),
firstWeekday = new Date(year, month, 1).getDay() - 1 //start on monday added -1
,
lastDateOfMonth = new Date(year, month + 1, 0).getDate(),
offsetDate = date.getDate() + firstWeekday - 1,
index = 1 // start index at 0 or 1, your choice
,
weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7),
week = index + Math.floor(offsetDate / 7);
if (exact || week < 2 + index) return week;
return week === weeksInMonth ? index + 5 : week;
};
//Generate day for current month
function generateDaysForMonth(year, month) {
var arr_days = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
var arr_date = [];
for (var x in arr_days) {
arr_date.push([month, arr_days[x], year].join('/'));
}
return arr_date;
}
function formatDateForSnow(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}