Home  >  Q&A  >  body text

How to get the number of days in a month

<p>I made a countdown timer and need to get the number of days in a month and subtract the number of days that have passed (eg: 30 days in September minus 8 days - today's date). Now I need to manually enter: </p> <pre class="brush:php;toolbar:false;">const end = new Date(2021, 8, 30, 13, 0,12, 12);</pre></p>
P粉354948724P粉354948724397 days ago517

reply all(2)I'll reply

  • P粉916553895

    P粉9165538952023-08-26 10:46:09

    You can use the following code to get the number of days in a month:

    const getDays = (year, month) => new Date(year, month, 0).getDate()
    
    const days = getDays(2021, 8)
    console.log(days)

    reply
    0
  • P粉792673958

    P粉7926739582023-08-26 10:42:00

    Number of days remaining in this month

    var currentDate = new Date();
    var currentYear = currentDate.getFullYear();
    var currentMonth = currentDate.getMonth();
    
    var currentMonthLastDate = (new Date(currentYear, currentMonth, 0)).getDate();
    
    var daysLeftInMonth = currentMonthLastDate - currentDate.getDate();
    
    console.log(daysLeftInMonth);

    reply
    0
  • Cancelreply