14

For my website I am trying to get the number of days for the CURRENT month for a certain feature.

I have seen examples online that get days of a specified month, however I need to get the days of the CURRENT month and find how many days are left of that month.

Here is the code I managed to put together:

function myFunction() {
    var today = new Date();
    var month = today.getMonth();
    console.log(month);
}

myFunction();
3
  • 2
    So, use the example online, and pass the current month to it...? Commented Jul 18, 2016 at 22:10
  • 3
    Possible duplicate of What is the best way to determine the number of days in a month with javascript? Commented Jul 18, 2016 at 22:10
  • And what exactly isn’t working with this code? Your code only gets the current month. Use it with whatever function you found that gets the number of days. Commented Jul 18, 2016 at 22:11

5 Answers 5

58

Does this do what you want?

function daysInThisMonth() {
  var now = new Date();
  return new Date(now.getFullYear(), now.getMonth()+1, 0).getDate();
}
Sign up to request clarification or add additional context in comments.

10 Comments

so how would i edit this to make it functional showing the days in the month?
I'm not sure what you mean. This function returns the number of days in the current month.
Maybe you need help logging it? E.g. console.log(daysInThisMonth());?
sorry let me reword it, I need to find the days remaining of the current month.
Oh, that would be daysInThisMonth() - (new Date()).getDate().
|
4

based on the answer from this post: What is the best way to determine the number of days in a month with javascript?

It should be easy to modify this to work for the current month Here's your code and the function from the other post:

function myFunction() {
    var today = new Date();
    var month = today.getMonth();
    console.log(daysInMonth(month + 1, today.getFullYear()))
}

function daysInMonth(month,year) {
  return new Date(year, month, 0).getDate();
}

myFunction();

Note that the function date.getMonth() returns a zero-based number, so just add 1 to normalize.

2 Comments

this is exactly what I was looking for thank you. any idea how to figure out how many days left in the current month?
Sure. Check out jsfiddle.net/8023vhfs Tell me if it helped you.
0

Get number of day's in current month: single line of code.

var daysInCurrentMonth=new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate()
console.log(daysInCurrentMonth);

Comments

0

Both lastday and date list of a month:

    function mliste(y=2024,m=2){//Here 2 means February
        var lastday = new Date(y,m,0).getDate();
        var daylist=[...Array(lastday+1).keys()]; daylist.shift();  
        return daylist; 
        //return lastday; 
    }
    console.log(mliste());

Comments

-1
var numberOfDaysOnMonth = function() {
    let h = new Date()
    h.setMonth(h.getMonth() + 1)
    h.setDate(h.getDate() - 1)
    return h.getDate()
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.