-2

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 :)

jboo
  • 1
  • Welcome to Stackoverflow. Could you please post what all you have tried so far? – susenj Jan 21 '21 at 15:53
  • 1
    If you don't know where to start, this would be a good place: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date – Randy Casburn Jan 21 '21 at 15:54
  • Hi, thanks I've take a look but for the moment in stuck to find the day from week and month parameter. I've try to use getDateOfISOWeek method but I only have the week number for the current month so it doesn't apply. – jboo Jan 21 '21 at 15:57
  • How do you define the week of the month? E.g. ISO 8601 defines the first week of the year as starting on the Monday before the first Thursday, so the first week of 2021 started on Monday, 4 January. – RobG Jan 21 '21 at 20:39
  • @jboo, working with dates in JS can be a real pain. I recommend you read the MDN docs linked by Randy above or look into using a Date library such at [Luxon](https://moment.github.io/luxon). – James Jan 21 '21 at 22:27
  • @James—a library won't help if the OP can't define what "1st week of February" means. – RobG Jan 21 '21 at 23:36
  • @RobG, assuming OP defines the starting week of the year/month following an established standard (there's a few!), then plenty of libraries will be able to work since they can deduce the starting day and week of the year. (If they know that Jan 1st, 1970 was a Thursday, they can work from there.) – James Jan 21 '21 at 23:57
  • @James—if you have a standard that defines how to calculate the week of a month (ISO 8601 doesn't), please post a link. If there are multiple standards (or algorithms, standard or otherwise) then the OP has to pick one. – RobG Jan 22 '21 at 01:09
  • Hi, I find this post that help me to find the week number in the month for a given date : https://stackoverflow.com/questions/3280323/get-week-of-the-month – jboo Jan 23 '21 at 08:43

1 Answers1

0

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('-');
}
jboo
  • 1