首頁  >  問答  >  主體

取得一個月的天數的方法

<p>我製作了一個倒數計時器,需要獲取一個月的天數並減去已經過去的天數(例如:九月的30天減去8天 - 今天的日期)。 現在我需要手動輸入:</p> <pre class="brush:php;toolbar:false;">const end = new Date(2021, 8, 30, 13, 0,12, 12);</pre></p>
P粉354948724P粉354948724397 天前518

全部回覆(2)我來回復

  • P粉916553895

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

    您可以使用以下程式碼取得一個月的天數:

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

    回覆
    0
  • P粉792673958

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

    本月剩餘天數

    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);

    回覆
    0
  • 取消回覆