How To Get Current Month Number Of Days in JavaScript?


Share:
Published By: Admin, Published On: Aug 04, 2021, Category: Javascript

Hello Friends,

Now let's see example of how to use javascript get current month number of days example. We will use how to use if get current month number of days in javascript. Here you will learn how to use javascript get current month number of days. This is a short guide on get current month number of days. Let's get started with how to get current month number of days in javascript.

Here i will give you many example how you can check javascript get current month number of days.

Example: 1
<!DOCTYPE HTML>
<html>
	<head>
		<title>Get Current Month Number Of Days Using JavaScript Example - MyWebtuts.com</title>	
		<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	</head>
	<body class="bg-dark">
		<div class="container mt-5">
			<div class="row">
				<div class="col-md-6 offset-md-3">
					<div class="card">
						<div class="card-header">
							<h4>JavaScript current month number of days</h4>
						</div>
						<div class="card-body">
							<p id = "emp1"></p>
							<button onclick = "cmp()" class="btn btn-primary mb-2">Check</button>
							<p id = "emp2"></p>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script>
			var up = document.getElementById('emp1');
			up.innerHTML = "Click on button to get the number"
					+ " of days in specified month";
			var down = document.getElementById('emp2');
			
			function daysInMonth (month, year) {
				return new Date(year, month, 0).getDate();
			}
			function cmp() {
				var date = new Date();
				var month = date.getMonth() + 1;
				var year = date.getFullYear();
				down.innerHTML = "Number of days in " + month
							+ "th month of the year " + year
							+" is "+ daysInMonth(month, year);
			}
		</script>
	</body>
</html>										
Output:
Number of days in 7th month of the year 2021 is 31
Example: 2
<!DOCTYPE HTML>
<html>
	<head>
		<title>Get Current Month Number Of Days Using JavaScript Example - MyWebtuts.com</title>	
		<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	</head>
	<body class="bg-dark">
		<div class="container mt-5">
			<div class="row">
				<div class="col-md-6 offset-md-3">
					<div class="card">
						<div class="card-header">
							<h4>JavaScript current month number of days</h4>
						</div>
						<div class="card-body">
							<p id = "GFG_UP"></p>
							<button onclick = "GFG_Fun()" class="btn btn-primary mb-2">Check</button>
							<p id = "GFG_DOWN"></p>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script>
			var up = document.getElementById('GFG_UP');
			up.innerHTML = "Click on button to get the"
				+ " number of days in specified month";
			var down = document.getElementById('GFG_DOWN');
			
			function daysInMonth (month, year) {
				return new Date(year, month, 0).getDate();
			}
			function GFG_Fun() {
				var date = new Date();
				var month = 7;
				var year = 2021;
				down.innerHTML = "Number of days in " + month
							+ "nd month of the year " + year
							+" is "+ daysInMonth(month, year);
			}
		</script>
	</body>
</html>										
Output:
Number of days in 7th month of the year 2021 is 31

It will help you....

Share: