How to get first Monday of month in JavaScript?

Are you looking for a way to get the first Monday of a month using JavaScript?

Look no further! In this article, we’ll show you how to do it with easy-to-follow code examples and explanations.

Keep reading to master this useful skill and learn how to easily find the first Monday of any month using JavaScript.

Solutions on how to get the first Monday of the Month in JavaScript

Here the following solutions to get first Monday of the Month in JavaScript.

Solution 1

This function takes a date string as an argument and returns the date of the first Monday of the month.

Here’s the example code where you can use to get the get first Monday of month in JS:

function findFirstMonday(dateString) {
  let targetDate = new Date(dateString);
  let targetMonth = targetDate.getMonth();
  let targetYear = targetDate.getFullYear();
  let firstDateInMonth = new Date(targetYear, targetMonth, 1);
  let firstWeekdayInMonth = firstDateInMonth.getDay();
  let firstMondayDate = 1 + ((8 - firstWeekdayInMonth) % 7);
  return new Date(targetYear, targetMonth, firstMondayDate).toLocaleDateString();
}

console.log(findFirstMonday("1 July 2023"));

This function accepts a date string as input and determines the date of the first Monday in that month.

It accomplishes this by creating a new Date object representing the first day of the specified month.

Using the getDay() method and some calculations, it determines the date of the first Monday.

The function then returns a formatted date string for that particular date.

For instance, if you invoke the function with the argument “1 July 2023”, it will create a Date object for July 1, 2023.

Since July 1, 2023 falls on a Saturday, the function calculates that the first Monday in July 2023 is July 3. Subsequently, it returns a formatted date string representing July 3, 2023.

Output:

7/3/2023

Solution 2

This function takes a year and a month as arguments and returns a Date object representing the first Monday of the specified month.

Here’s another example code:

function getFirstMonday(year, month) {
  let date = new Date(year, month - 1, 1);
  while (date.getDay() !== 1) {
    date.setDate(date.getDate() + 1);
  }
  return date;
}

let firstMonday = getFirstMonday(2023, 7);
console.log(`The first Monday of the month is: ${firstMonday.toLocaleDateString()}`);

The solution I provided is a function that takes a year and a month as arguments and returns a Date object representing the first Monday of the specified month.

The function creates a new Date object representing the first day of the specified month, then uses a while loop to increment the date until it falls on a Monday.

Once the date falls on a Monday, the function returns the Date object representing that date.

Output:

The first Monday of the month is: 7/3/2023

How to get the first Monday of a month using moments.js?

You can use the moment.js library to find the first Monday of a month. Here’s an example of how you can do it:

const moment = require('moment');

function getFirstMonday(dateString) {
  let date = moment(dateString, 'YYYY-MM-DD');
  let firstMonday = date.startOf('month').day("Monday");
  if (firstMonday.date() > 7) firstMonday.add(7, 'd');
  return firstMonday.format('L');
}

console.log(getFirstMonday('2023-07-01'));

This function takes a date string in the format YYYY-MM-DD as an argument and returns the date of the first Monday of the month.

For instance, getFirstMonday(‘2023-07-01’) would return 07/03/2023.

Output:

07/03/2023

How to get first Monday and last Monday of the month?

If you wanted to get the first day Monday and last Monday of the month here’s the example code:

function getFirstAndLastMonday(dateString) {
  let targetDate = new Date(dateString);
  let targetMonth = targetDate.getMonth();
  let targetYear = targetDate.getFullYear();
  let firstDateInMonth = new Date(targetYear, targetMonth, 1);
  let firstWeekdayInMonth = firstDateInMonth.getDay();
  let firstMondayDate = 1 + ((8 - firstWeekdayInMonth) % 7);
  let lastDateInMonth = new Date(targetYear, targetMonth + 1, 0);
  let lastWeekdayInMonth = lastDateInMonth.getDay();
  let lastMondayDate = lastDateInMonth.getDate() - ((lastWeekdayInMonth + 6) % 7);
  return {
    firstMonday: new Date(targetYear, targetMonth, firstMondayDate).toLocaleDateString(),
    lastMonday: new Date(targetYear, targetMonth, lastMondayDate).toLocaleDateString()
  };
}

console.log(getFirstAndLastMonday("1 july 2023")); 

This function takes a date string as an argument and returns an object with the date of the first and last Monday of the month. For example, getFirstAndLastMonday(‘1 jul 2023’) would return {firstMonday: “7/3/2023”, lastMonday: “7/31/2023”}.

Output:

{ firstMonday: '7/3/2023', lastMonday: '7/31/2023' }

Conclusion

In conclusion, this article presented various solutions to get the first Monday of a month in JavaScript.

By following the provided code examples and explanations, you can quickly and easily implement these solutions to obtain the desired result.

Whether it’s using basic JavaScript functions or leveraging external libraries like moment.js, JavaScript offers flexible approaches to calculate the first Monday of any given month.

With this newfound knowledge, you can efficiently work with dates and incorporate this functionality into their applications as needed.

We are hoping that this article provides you with enough information that helps you understand the JavaScript get first Monday of month.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment