Friday, November 12, 2021

JavaScript useful Date related (week,month,quarter) functions

As we all seen JavaScript is the most preferred programming language of the Internet.

Here are someuseful functions to iterate through the weeks / months / quarters in your code using simple JS functions.

    const weekRange = (num) => {
        // num defintion: -1 is pevious week, likewise -2,-3 OR only 1, 2 for upcoming weeks.
        let mydate = new Date();
        let mydate2 = new Date();
        num = num * 7; // 7 days per week * counter
        let x = mydate.getDate() - mydate.getDay() + 1; // +1 to set the first day of the week as Monday
        mydate.setDate(x + num);
        mydate.setHours(0, 0, 0); // setting the 0,0,0 to cover entire week right from day one midnight
        mydate2.setDate(x + num + 5 + 1); // +6 to calculatethe Saturday EOD
        mydate2.setHours(23, 59, 0); // setting 23,59 tp cover last day of the week till midnight
        return [mydate, mydate2]; //return array to compare the window with
    };

    const monthName = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ];

    const browseMonths = (num) => {
        // pass number of months you wish to add or subtract.
        let DateObj = new Date();
        DateObj.setMonth(DateObj.getMonth() + num);
        return DateObj;
    };


    const browseQuarter = (num) => {
        // num is ctr passed to calculate the future or past months
        let DateObj = new Date();
        let qstart = new Date();
        let qend = new Date();
        let quarter = "";
        num = parseInt(num);

        if (num !== 0) {
            DateObj.setMonth(DateObj.getMonth() + num * 3);
        } else {
            DateObj.setMonth(DateObj.getMonth());
        }
        const month = parseInt(DateObj.getMonth());

        if (month >= 0 && month <= 2) {
            quarter = "Q1 " + DateObj.getFullYear();
            //console.log(DateObj.toDateString());
            qstart = new Date(DateObj);
            qend = new Date(DateObj);
            qstart.setMonth(0);
            qstart.setDate(1);
            qstart.setHours(0, 0, 0);
            qend.setMonth(2);
            qend.setDate(31);
            qend.setHours(23, 59, 0);
        } else if (month >= 3 && month <= 5) {
            quarter = "Q2 " + DateObj.getFullYear();
            //console.log(DateObj.toDateString());
            qstart = new Date(DateObj);
            qend = new Date(DateObj);
            qstart.setMonth(3);
            qstart.setDate(1);
            qstart.setHours(0, 0, 0);
            qend.setMonth(5);
            qend.setDate(30);
            qend.setHours(23, 59, 0);
        } else if (month >= 6 && month <= 8) {
            quarter = "Q3 " + DateObj.getFullYear();
            //console.log(DateObj.toDateString());
            qstart = new Date(DateObj);
            qend = new Date(DateObj);
            qstart.setMonth(6);
            qstart.setDate(1);
            qstart.setHours(0, 0, 0);
            qend.setMonth(8);
            qend.setDate(30);
            qend.setHours(23, 59, 0);
        } else if (month >= 9 && month <= 11) {
            quarter = "Q4 " + DateObj.getFullYear();
            //console.log(DateObj.toDateString());
            qstart = new Date(DateObj);
            qend = new Date(DateObj);
            qstart.setMonth(9);
            qstart.setDate(1);
            qstart.setHours(0, 0, 0);
            qend.setMonth(11);
            qend.setDate(31);
            qend.setHours(23, 59, 0);
        } else {
            quarter = "";
        }
        return [quarter, qstart, qend];
    };