2011-02-03 130 views
11

我有一個包含月份,日期和年份的3個下拉菜單的HTML頁面,我想知道是否有一種方法可以根據月份正確填充月份下拉菜單和一年。Javascript:計算給定年份的月份天數

我以前沒有在客戶端做過這件事,但它看起來像很多像jQuery DatePicker這樣的控件都是在幕後做的。

+1

可能重複:在選擇框,重新填充日期(http://stackoverflow.com/questions/4822550/repopulating-dates-on-select-boxes) – 2011-02-03 01:50:50

+0

感謝BOX9!這實際上是我在尋找的。 – Abe 2011-02-03 01:54:36

回答

18

您可以用Date對象玩:

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

算術與Date對象給出的毫秒數。

這甚至可以在12月份工作; Date構造函數通過環繞處理超出範圍的參數。

注意month是從零開始的(它必須是0之間11

25

據我所知,沒有(整潔的)內置函數。我寫這一次,

// note that month is 0-based, like in the Date object. Adjust if necessary. 
function getNumberOfDays(year, month) { 
    var isLeap = ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)); 
    return [31, (isLeap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]; 
} 
+1

這很酷,你可以在數組中包含一個三元語句!太好了! – Abe 2011-02-03 01:53:54

+1

這應該是答案!輝煌! – Johny 2016-01-28 20:09:12

2
Date.prototype.daysinMonth: function(){ 
    var d= new Date(this.getFullYear(), this.getMonth()+1, 0); 
    return d.getDate(); 
} 

function daysinMonthfromInput(month,year){ 
    return (new Date(year,month-1,1)).daysinMonth(); 
} 

alert(daysinMonthfromInput(2,2011)); 
1

下面是一行。 假設你說的一月= 1,二月= 2等。(是正常的) 下面是閏年例如:

var y = 2012; 
var m = 2; 
var daysInMonth = new Date(y,m,1,-1).getDate(); 
0

我使用這種方法在我當前的項目,我發現我需要正確的圓關閉錯誤。因此,而不是在我的代碼使用monthLength,我不得不使用它代替:

monthLength.toFixed(0) 

例如,如果我有一個對象,其中我存儲文本日期字段,它可能是這樣的:

obj.month = theMonths[mm - 1] + " " + monthLength.toFixed(0) + ", " + obj.year; 
從另一篇文章
2

複製:Get number days in a specified month using javascript?

//Month is 1 based 
function daysInMonth(month,year) { 
return new Date(year, month, 0).getDate(); 
} 

//July 
daysInMonth(7,2009); //31 
//February 
daysInMonth(2,2009); //28 
daysInMonth(2,2008); //29 

所有學分@c_harm,真正偉大的解決方案

0

你可以使用這個:

var curdate = new Date(); Days Date = 32 - new Date(curdate.getYear(),curdate.getMonth(),32).getDate();

;)

-2
Date.prototype.daysinMonth= function(){ 
var d= new Date(this.getFullYear(), this.getMonth()+1, 0); 
return d.getDate(); 
}; 

function daysinMonthfromInput (month, year) { 
    return (new Date(year, month - 1, 1)).daysinMonth(); 
}; 
function fillallday (elem, month, year) { 
    var options = null; 
    var elementExists = document.getElementById(elem); 

    if (elementExists != null) { 

     this.removeOptions(elementExists); 
     var opt = document.createElement('option'); 
     opt.value = ""; 
     opt.innerHTML = "---Day---"; 
     elementExists.appendChild(opt); 
     if (month != "") { 
      if (typeof (year) === "undefined") { 
       year = new Date().getFullYear(); 
      } 
      if (year == "") { 
       year = new Date().getFullYear(); 
      } 
      var days = daysinMonthfromInput(month, year); 
      for (var i = 1; i <= days; i++) { 
       var opt = document.createElement('option'); 
       opt.value = i; 
       opt.innerHTML = i; 
       elementExists.appendChild(opt); 
      } 
     } 
    } 

}