How do you get the first working day of the month in javascript?

How can I write a function that returns the date object of the following month's first workday (monday through friday) without using Moment.js? this code gets me the last businesss day of the month but i'd like to switch it to get me the first day of next month.

function lastBusinessDayOfMonth(year, month) {
var date = new Date();
var offset = 0;
var result = null;

if ('undefined' === typeof year || null === year) {
    year = date.getFullYear();
}

if ('undefined' === typeof month || null === month) {
    month = date.getMonth();
}

do {
    result = new Date(year, month, offset);

    offset--;
} while (0 === result.getDay() || 6 === result.getDay());

return result;

}

asked Dec 17, 2018 at 21:04

3

You can generate a date for the first of the month, and if it's a Sunday add one day, if it's a Saturday add two days.

You should also consider the name carefully. Not every Monday to Friday is a business day as some are holidays, and in many cultures and occupations the business week is not Monday to Friday.

/*  Return first day in the following month that is Monday to Friday,
 *  or not Saturday or Sunday.
 * 
 *  @param {number|string} year - defaults to current year
 *  @param {number|string} month - defaults to current month
 *  @returns {Date} - first day of following month that is Monday to Friday
 */
function firstMonToFriNextMonth(year, month) {
  var d = new Date();
  d = new Date(Number(year)  || d.getFullYear(),
               Number(month) || d.getMonth() + 1,
               1);
  d.getDay() % 6? d : d.setDate((2 + d.getDay()) % 5);
  return d;
}

// No args
console.log(firstMonToFriNextMonth().toString());
// Year only
console.log(firstMonToFriNextMonth(2018).toString());
// Month only
console.log(firstMonToFriNextMonth(null, 1).toString());
// All of 2018
for (var i=1; i<13; i++) {
  console.log(firstMonToFriNextMonth(2018,i).toString());
}

answered Dec 18, 2018 at 0:58

RobGRobG

137k30 gold badges167 silver badges205 bronze badges

If starting at an offset = 0 starts you at the last day of the month, then an offset of 1 will start you and the first day of the next month. And instead of working backwards with a offset--, you work forwards with an offset++.

function lastBusinessDayOfMonth(year, month) {
var date = new Date();
var offset = 1;
var result = null;

if ('undefined' === typeof year || null === year) {
    year = date.getFullYear();
}

if ('undefined' === typeof month || null === month) {
    month = date.getMonth();
}

do {
    result = new Date(year, month, offset);

    offset++;
} while (0 === result.getDay() || 6 === result.getDay());

return result;

}

answered Dec 17, 2018 at 21:42

Neil.WorkNeil.Work

9637 silver badges9 bronze badges

0

Date functions in JavaScript play a very important role in web applications. It not only provides date and month information, but also allows us to calculate and get results based on date related functions. I am going to show you how to get the first and last day of a given month using JavaScript functions.

It’s a very common scenario. Let’s say I have a DatePicker control on my web page. Whenever a user selects a date from the DatePicker, it would call a JavaScript function that will get the first and last day (day in words) of the month.

See this demo

For the DatePicker, I would prefer using HTML5 input type date. This is a very useful control. However, I recommend you please check browser capabilities of this element. In-addition, check the below Related link to know more about the HTML5 input type date.

👉 Do you know you can get the first and last day of a month or date in SQL Server? Yes you can and its very simple. Check this post.

How do you get the first working day of the month in javascript?

The Complete Code

The onmouseout event of the input type element will call a user defined function getTheDay() written in the

Try it

The above script has 3 JavaScript date functions. I am first creating an object of the Date() method. Instantiating the Date() method with new keyword will ensure that it is a date function (not a string) and I’ll have access to all the methods and properties it has.

HTML5 input type date and other important Attributes

JavaScript getFullYear() and getMonth Methods

Next, I need the month and year for the selected date, so I can calculate the first and last day of the month. Method getFullYear() will return a four digit year for the selected date. The method getMonth() will return a value between 0 and 11 for month.

Once I got the month and year for selected date, I now need to get the first and last day of the month. Again I am using the Date() method twice to get the desired result.

var FirstDay = new Date(year, month, 1);
var LastDay = new Date(year, month + 1, 0);

In both the above methods, I am passing 3 parameters each, and the first 2 are year and month. The third parameter in the first method indicates the day (day one), and it will return the first day of the month.

The second method is slightly tricky. I have assigned + 1 to the month and 0 for the day. This will return the last day of the month. For example, if you select a date for the month of May, adding + 1 would return 5, that is, June. However, adding 0 for the day would return 4, since there is no 0 day for the month of June.

How to show date and time as Blank instead of "1900-01-01" in SQL Server

JavaScript getDay() Method

Finally, we’ll get the days (in words) from the first and last date. To do this I am using JavaScript getDay() method. This method will return the “day” (starting Zero) of the week for the selected date. However, it returns a number and I need to convert the number (for days) in words. Therefore, I have declared an array and stored the weekdays in it. Since the getDay() method returns number starting with 0 for Sunday and so on, I have added values to array starting with Sunday.

See this demo

That’s it. Don’t forget to check the demo. Thanks for reading.

JavaScript functions you must know!

1) ES6 map() function Example: The map() function creates a new array from another array. It uses the same elements from the parent array and creates a new array of elements by calling a function.

2) JavaScript filter() method: Learn about the filter() method in JavaScript and find out why this method is more effective and useful than other methods.

3) JavaScript setInterval() method: The JavaScript setInterval() method calls a function or executes a code repeatedly at specified time intervals.

← PreviousNext →


How do you get the first date of the month from a date in JavaScript?

To get the first and last day of the current month, use the getFullYear() and getMonth() methods to get the current year and month and pass them to the Date() constructor to get an object representing the two dates. Copied! const now = new Date(); const firstDay = new Date(now. getFullYear(), now.

How do you know if it's the first day of the month?

To check if a date is the first day of the month: If the result is equal to 1 , the date is the first day of the month.

How do you find the first Monday of the month in JavaScript?

“get first monday of month javascript” Code Answer's.
var date = new Date();.
var firstDay = new Date(date. getFullYear(), date. getMonth(), 1);.
var lastDay = new Date(date. getFullYear(), date. getMonth() + 1, 0);.

What is the first day of the week JavaScript?

To get the first day of the current week, get the day of the month, subtract the day of the week and add 1 to consider Monday the first day of the week. Copied! function getFirstDayOfWeek(d) { // 👇️ clone date object, so we don't mutate it const date = new Date(d); const day = date.