skip to Main Content

I have a list of dates and want to check how often occurs weekend, weeks and month in the list

I have a list of dates from 1 year like 2023-01-01 until 2024-01-01

["2023-01-31T23:00:00.000Z", "2023-02-01T23:00:00.000Z", "2023-02-02T23:00:00.000Z", "2023-02-03T23:00:00.000Z", …]

How can I convert this that I get a object of this:

day: 12,
weekend: 5,
month: 5

3

Answers


  1. Add all your data inside an array, and then using the forEach method, iterate over each date string.

    There are 3 main variables.

    1. dayCount to count the total number of days.
    2. weekendCount to count the number of weekends.
    3. monthCount to count the number of months.

    Count the total number of days by increasing the dayCount.

    If the date falls on a weekend (Saturday or Sunday), we increase weekendCount to keep track of the total number of weekends.

    If the date is the first day of the month, increase monthCount to keep track of the number of months.

    You’re welcome, here’s your final code.

    const dateList = [
      "2023-01-31T23:00:00.000Z",
      "2023-02-01T23:00:00.000Z",
      "2023-02-02T23:00:00.000Z",
      "2023-02-03T23:00:00.000Z",
      // modify your array
    ];
    
    let dayCount = 0;
    let weekendCount = 0;
    let monthCount = 0;
    
    dateList.forEach((dateStr) => {
      const date = new Date(dateStr);
      dayCount++;
    
      // check if the current date is a weekend (Saturday or Sunday)
      if (date.getDay() === 0 || date.getDay() === 6) {
        weekendCount++;
      }
    
      // check if the current date is the first day of the month
      if (date.getDate() === 1) {
        monthCount++;
      }
    });
    
    console.log("Days:", dayCount);
    console.log("Weekends:", weekendCount);
    console.log("Months:", monthCount);

    You can make your code a little bit shorter by using filter method, and the dayCount is simply set to the length of the dateList array (guessing that each date is a new day).

    const dateList = [
      "2023-01-31T23:00:00.000Z",
      "2023-02-01T23:00:00.000Z",
      "2023-02-02T23:00:00.000Z",
      "2023-02-03T23:00:00.000Z",
      // modify your array
    ];
    
    // count the total number of days
    let dayCount = dateList.length;
    
    // count the number of weekends
    let weekendCount = dateList.filter(dateStr => {
      const date = new Date(dateStr);
      return date.getDay() === 0 || date.getDay() === 6; // Sunday is 0, and Saturday is 6
    }).length;
    
    // count the number of months
    let monthCount = dateList.filter(dateStr => {
      const date = new Date(dateStr);
      return date.getDate() === 1; // check if it's the first day of the month
    }).length;
    
    console.log("Days:", dayCount);
    console.log("Weekends:", weekendCount);
    console.log("Months:", monthCount);

    You can make your code even shorter by using an arrow function expression, and directly assigning variables.

    const dateList = [
      "2023-01-31T23:00:00.000Z",
      "2023-02-01T23:00:00.000Z",
      "2023-02-02T23:00:00.000Z",
      "2023-02-03T23:00:00.000Z",
      // modify your array
    ];
    
    let dayCount = dateList.length;
    let weekendCount = dateList.filter(dateStr => new Date(dateStr).getDay() % 6 === 0).length;
    let monthCount = dateList.filter(dateStr => new Date(dateStr).getDate() === 1).length;
    
    console.log("Days:", dayCount);
    console.log("Weekends:", weekendCount);
    console.log("Months:", monthCount);
    Login or Signup to reply.
  2. use momentjs to manipulate date in javascript

    Date of Month :

    moment().date(Number);
    moment().date(); // Number
    moment().dates(Number);
    moment().dates(); // Number
    

    Day of Week :

    moment().day(Number|String);
    moment().day(); // Number
    moment().days(Number|String);
    moment().days(); // Number
    
    Login or Signup to reply.
  3. You can achieve this by following the steps below:

    • Parse each date string in your array into a Date object.

    • Keep three counters: one for days, one for weekends, and one for months.

    • Check each date. If it’s a weekend (Saturday or Sunday), increment the weekend counter.

    • Keep track of the current month. When the month changes, increment the month counter.

    • Finally, return an object containing the three counts.

       function countDaysWeekendsMonths(dates) {
          let dayCount = 0;
          let weekendCount = 0;
          let monthCount = 0;
      
          let currentMonth = null;
      
          for (const dateString of dates) {
            const date = new Date(dateString);
            dayCount++;
      
            const dayOfWeek = date.getDay();
            if (dayOfWeek === 0 || dayOfWeek === 6) { // 0 is Sunday, 6 is Saturday
              weekendCount++;
            }
      
            const month = date.getMonth();
            if (month !== currentMonth) {
              currentMonth = month;
              monthCount++;
            }
          }
      
          return {
            day: dayCount,
            weekend: weekendCount,
            month: monthCount
          };
        }
      

    //example

        const dates = ["2023-01-01T23:00:00.000Z", "2023-01-02T23:00:00.000Z", "2023-01-03T23:00:00.000Z", "2023-01-04T23:00:00.000Z", "2023-01-05T23:00:00.000Z", "2023-01-06T23:00:00.000Z"];
        console.log(countDaysWeekendsMonths(dates));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search