-1

I want to get all the start and the end dates of weeks of a year starting 2022 in moment in YYYY-MM-DD (mysql) format.

I want the end result to look something like this:

[
    ['2022-01-04', '2022-01-10'],
    ['2022-01-11', '2022-01-17'],
    ['2022-01-18', '2022-01-24'],
    // ...
    ['2022-12-24', '2023-01-01'],
    ['2023-01-02', '2023-01-08'],
    // ...
    ['2023-12-25', '2024-01-01'],
    // ...
    // Until the end of 2026
];

EDIT 1: I tried the code below but it's failing:

const moment = require('moment');

const dateFormat = 'YYYY-MM-DD';
const dateToStart = '2022-04-11';
const dateToEnd = '2030-12-29';

function getWeeks() {
    const weeks = [];
    let currentDate = '2022-04-11';

    while (true) {

        if (currentDate === dateToStart) {
            weeks.push([dateToStart, moment(dateToStart, dateFormat).add(6, 'days').format(dateFormat)]);
        } else {
            weeks.push([
                moment(currentDate, dateFormat).add(1, 'days').format(dateFormat),
                moment(currentDate, dateFormat).add(6, 'days').format(dateFormat),
            ]);
        }
        currentDate = moment(dateToStart).add(6, 'days').format(dateFormat);

        if (currentDate === dateToEnd) break;
    }

    return weeks;
}
Cedric Hadjian
  • 849
  • 12
  • 25
  • You should at least show us that you made an attempt to figure this out for yourself. – phuzi Apr 06 '22 at 10:06
  • @phuzi I'm not much familiar with moment – Cedric Hadjian Apr 06 '22 at 10:48
  • @phuzi check my edit please, thanks. Would also appreciate it if you could remove the -1 and close request. – Cedric Hadjian Apr 06 '22 at 14:11
  • you don't say what's not working, or why it is not working. put in some console.log() and trace what it is doing - step thru it to debug. additionally moment is very powerful - e.g. moment().startOf('week') might be something you could use, a.o. methods; – batman567 Apr 06 '22 at 14:18
  • `[ ['2022-01-04', '2022-01-10'], ['2022-01-11', '2022-01-17'], ...]` <-- seems like a rather straight-forward pattern here. `[ ["week-start-date-of-year", "+6 days to curr-elt index-0"], ["+1 day to prev-elt index-1", "+6 days to curr-elt index-0"], ....]`. – jsN00b Apr 06 '22 at 14:26

1 Answers1

1

Momentjs provide some really usefull functions to get the desired output.

  1. Create 2 moment objects, the start and end
  2. Loop, while end isAfter start
  3. Use .startOf('isoWeek') and .endOf('isoWeek') to get the range of a week
  4. Append that to an array
  5. Set the start moment object 1 week forward: .add(1, 'week')

This example should get you started, note that I've lowered the dateToEnd.

const dateFormat = 'YYYY-MM-DD';
const dateToStart = '2022-04-01';
const dateToEnd = '2023-12-29';

let weeks = [];
let momsrt = moment.utc(dateToStart, dateFormat);
let momend = moment.utc(dateToEnd, dateFormat);

while (momend.isAfter(momsrt)) {
    weeks.push([
        momsrt.startOf('isoWeek').format(dateFormat),
        momsrt.endOf('isoWeek').format(dateFormat)
    ]);
    momsrt.add(1, 'week');
}

console.log(weeks);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.32/moment-timezone-with-data.min.js"></script>

Foot Note:

Please see momentjs Project status

TL:DR; Don't use it for new projects, there are way better alternatives.

0stone0
  • 34,288
  • 4
  • 39
  • 64