Get Number Days in a Specified Month Using JavaScript

What is the best way to determine the number of days in a month with JavaScript?

function daysInMonth (month, year) { // Use 1 for January, 2 for February, etc.
return new Date(year, month, 0).getDate();
}

console.log(daysInMonth(2, 1999)); // February in a non-leap year.
console.log(daysInMonth(2, 2000)); // February in a leap year.

get number of days in the CURRENT month using javascript

Does this do what you want?

function daysInThisMonth() {
var now = new Date();
return new Date(now.getFullYear(), now.getMonth()+1, 0).getDate();
}

Javascript: calculate number of days in month for a given year

You can play with date objects:

var monthStart = new Date(year, month, 1);
var monthEnd = new Date(year, month + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24)

Arithmetic with Date objects gives a number of milliseconds.

This will even work for December; the Date constructor handles out-of-range arguments by wrapping around.

Note that month is zero-based (it must be between 0 and 11)

Find all the days in a month with Date object?

To get a list of all days in a month, you can start with a Date on the first day of a month, increase the day until the month changes.

/**
* @param {int} The month number, 0 based
* @param {int} The year, not zero based, required to account for leap years
* @return {Date[]} List with date objects for each day of the month
*/
function getDaysInMonth(month, year) {
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
days.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return days;
}

UTC Version

In response to some comments, I've created a version that uses UTC methods in case you want to call UTC methods instead of the standard methods that return the localized time zone.

I suspect this is the culprit of the comments saying this didn't work. You typically want to make sure you call getUTCMonth/Day/Hours methods if you instantiated it with Date.UTC, and vice-versa, unless you are trying to convert time zones and show differences.

function getDaysInMonthUTC(month, year) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getUTCMonth() === month) {
days.push(new Date(date));
date.setUTCDate(date.getUTCDate() + 1);
}
return days;
}

Editing This Answer

If you think there's a problem with this script, please feel free to:

  • First see existing unit tests below
  • Write a test case that proves it's broken.
  • Fix the code, making sure existing tests pass.

Unit Tests

/**
* @param {int} The month number, 0 based
* @param {int} The year, not zero based, required to account for leap years
* @return {Date[]} List with date objects for each day of the month
*/
function getDaysInMonthUTC(month, year) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getUTCMonth() === month) {
days.push(new Date(date));
date.setUTCDate(date.getUTCDate() + 1);
}
return days;
}

function getDaysInMonth(month, year) {
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
days.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return days;
}

const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

describe("getDaysInMonthUTC", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonthUTC(index, 2020).length
);
expect(actual).toEqual(days2020);
});

it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonthUTC(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});

describe("getDaysInMonth", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonth(index, 2020).length
);
expect(actual).toEqual(days2020);
});

it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonth(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});

// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<link href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" rel="stylesheet"/>

the number of days in the given month of the given year

This should work:

function isLeapYear(year){
return(year%4==0);
}
console.log(isLeapYear(2024));
console.log(isLeapYear(2023));
function daysInMonth(month,year){
if (month!=2){
a=[1,3,5,7,8,10,12]
for (x in a){
if (month==a[x]){
return(31)
}
}
return(30);
}else{
if (isLeapYear(year)){
return(29);
}else{
return(28)
}
}
}
console.log(daysInMonth(2,2024))
console.log(daysInMonth(2,2023))
console.log(daysInMonth(1,2023))
console.log(daysInMonth(4,2023))

[edit]
Sorry about that.
Anyway, the first function returns true if the year is a leap year.
The second function returns the amount of days in the specified month of the year.
That should be better.

How Get number days in a specified month using jQuery JavaScript for display table?

could you provide one example in which way you want to show the data?

Let's take one example in the first form if you select the year 2019 and month 7 then what should display.

please explain in details this will be helpful.

Calculate last day of month

var month = 0; // January
var d = new Date(2008, month + 1, 0);
console.log(d.toString()); // last day in January

D3.js - get number of days for a certain month

You're misreading the documentation on intervals : the number of days in the intervals used by d3.time.month have a length between 28 and 31 but the functions are defined as

interval(date)

Alias for interval.floor(date). [...]

and

interval.floor(date)

Rounds down the specified date, returning the latest time interval
before or equal to date. [...]

Basically, d3.time.month(date) will return the first day of the month at midnight, not the number of days in that month.


How to get the number of days then? As far as I can tell, D3 does not expose a way to get the length of the month for a given date. You could of course get the range of days for a given month and extract its length:

var date = new Date(2016, 01, 02); // 2016-02-02
console.log(
d3.time.days(d3.time.month(date), d3.time.month.ceil(date)).length
)

or probably more efficiently use plain JS like in this answer https://stackoverflow.com/a/1185804/1071630 :

var date = new Date(2016, 01, 02); // 2016-02-02
console.log(
new Date(date.getFullYear(), date.getMonth()+1, 0).getDate()
)

var date = new Date(2016, 01, 02);
console.log( d3.time.days(d3.time.month(date), d3.time.month.ceil(date)).length)
console.log( new Date(date.getFullYear(), date.getMonth()+1, 0).getDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>


Related Topics



Leave a reply



Submit