月、日、年の3つのドロップダウンがあるHTMLページがあり、月に応じてドロップダウンを適切に挿入する方法があるかどうかは疑問でしたそして年。JavaScript:特定の年の月の日数を計算する
私はこれまでにクライアント側でこれを行っていませんが、jQuery DatePickerのような多くのコントロールのように見えます。
月、日、年の3つのドロップダウンがあるHTMLページがあり、月に応じてドロップダウンを適切に挿入する方法があるかどうかは疑問でしたそして年。JavaScript:特定の年の月の日数を計算する
私はこれまでにクライアント側でこれを行っていませんが、jQuery DatePickerのような多くのコントロールのように見えます。
あなたは日付オブジェクトで遊ぶことができます。
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
の間でなければならない)
これはすばらしいことです。閏年でも動作します。私は2/2012を試しました。ありがとう! – Abe
私は 'var DaysInMonth = function(年、月){/ *ここにSLAKのコードを* /; date.prototype.daysInMonth = function(){var mlen = DaysInMonth(this.getFullYear(this、this.getMonth());}にプロトタイプメソッドを追加しました。リターンmlen; }; '私は呼び出すことができるので'(新しいDateTime(2000、1))。daysInMonth(); ' –
3月2014年は何らかの理由で30.958333333333332を返します。 – o01
私が知る限り、そのための(組み込みの)組み込み関数はありません。私はこの一度書いた:
// 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];
}
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月= 1、2月= 2など。 ここ閏年の例を示します(正常である):
var y = 2012;
var m = 2;
var daysInMonth = new Date(y,m,1,-1).getDate();
私は私の現在のプロジェクトで、このアプローチを使用して、私はラウンドのために正しい必要なことがわかっていますオフエラー。私は、テキストの日付フィールドを格納していているオブジェクトがある場合、それはこのように見えるかもしれ例えば
monthLength.toFixed(0)
:
obj.month = theMonths[mm - 1] + " " + monthLength.toFixed(0) + ", " + obj.year;
を代わりに私のコードでmonthLengthを使用するので、私の代わりにこれを使用する必要がありました
別のポストからコピー: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するすべてのクレジット、本当に素晴らしいソリューション
実際にはこれを使用できます:
var curdate = new Date(); DaysMonth = 32 - 新しい日付(curdate.getYear()、curdate.getMonth()、32).getDate();
;)
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);
}
}
}
}
この質問は既に正しく答えられています。元の回答には何が追加されますか? –
可能な重複:[選択ボックスに日付を再増殖](http://stackoverflow.com/questions/4822550/repopulating-dates-on-select-boxes) –
おかげBox9!それは実際に私が探していたものです。 – Abe