2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

日付をプルダウンで表示する。(javascript)

Posted at

スクリーンショット 2017-12-11 22.56.57.png

忘れっぽいので、メモとして残しておく。

###HTML

date.html
<html>
<head>
<meta charset="utf-8" />
<script src="getDate.js"></script>
</head>
<body>
<b>今日の日付</b><br />
<a id="year"></a><a id="month"></a><a id="day"></a>
</body>
</html>

###Javascript

getDate.js
window.onload = function getDate(){
	var yyyy, mm, dd, today
	today = new Date();
	yyyy = today.getFullYear();
	mm = today.getMonth() + 1;
	dd = today.getDate();
	
	//年
	$Year = "<select>";
	for( var i=2000; i<=2030; i++ ){
		if( i == yyyy){
			$Year += "<option value=\"" + i + "\" selected >" + i + "</option>";
		}else{
			$Year += "<option value=\"" + i + "\" >" + i + "</option>";
		}
	}
	$Year += "</select>";
	document.getElementById("year").innerHTML = $Year + "";
	
	//月
	$Month = "<select>";
	for( var i=1; i<=12; i++ ){
		if( i == mm){
			$Month+= "<option value=\"" + i + "\" selected >" + i + "</option>";
		}else{
			$Month+= "<option value=\"" + i + "\" >" + i + "</option>";
		}
	}
	$Month += "</select>";
	document.getElementById("month").innerHTML = $Month + "";
	
	//日付
	$Day = "<select>";
	for( var i=1; i<=31; i++ ){
		if( i == dd){
			$Day+= "<option value=\"" + i + "\" selected >" + i + "</option>";
		}else{
			$Day+= "<option value=\"" + i + "\" >" + i + "</option>";
		}
	}
	$Day += "</select>";
	document.getElementById("day").innerHTML = $Day + "";
}
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?