Javascript Get Current Month Start And End Date Example


Share:
Published By: Admin, Published On: Jul 28, 2021, Category: Javascript
Hello Friends, Now let's see example of how to use javascript get current month start and end date example. We will use how to use if get current month start and end date in javascript. Here you will learn how to use javascript get current month start and end date. This is a short guide on get current month start and end date. Let's get started with how to get current month start and end date in javascript. Here i will give you many example how you can check javascript get current month start and end date. Example: 1
<!DOCTYPE HTML>
<html>
	<head>
		<title>Get first and last date of current month</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">
							<h5>How To Get Current Time In Javascript?</h5>
						</div>
						<div class="card-body">
							<p id = "cmp1"></p>
							<button onclick = "cmprun()" class="btn btn-primary mb-2">click here</button>
							<p id = "cmp2"></p>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script>
			var up = document.getElementById('cmp1');
			up.innerHTML = "Click on button to get the first "
					+ "and last day of current month";
			var down = document.getElementById('cmp2');
			
			function cmprun() {
				var date = new Date();
				var firstDay =
					new Date(date.getFullYear(), date.getMonth(), 1);
				var lastDay =
				new Date(date.getFullYear(), date.getMonth() + 1, 0);
				down.innerHTML = "First day = " + firstDay +
							"<br>Last day = " + lastDay;
			}
		</script>
	</body>
</html>					
Output:
First day = Thu Jul 01 2021 00:00:00 GMT+0530 (India Standard Time)
Last day = Sat Jul 31 2021 00:00:00 GMT+0530 (India Standard Time)
Example: 2
<!DOCTYPE HTML>
<html>
	<head>
		<title>Get first and last date of current month</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">
							<h5>How To Get Current Time In Javascript?</h5>
						</div>
						<div class="card-body">
							<p id = "cmp1"></p>
					        <button onclick = "cmprun()" class="btn btn-primary mb-2">click here</button>
					        <p id = "cmp2"></p>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script>
			var up = document.getElementById('cmp1');
            up.innerHTML = "Click on button to get the first "
                    + "and last day of current month";
            var down = document.getElementById('cmp2'); 
              
            function daysInMonth (month, year) {
                return new Date(year, month, 0).getDate();
            }
            function cmprun() {
                var date = new Date();
                var firstDay = new Date(date.getFullYear(),
                                date.getMonth(), 1);
                var lastDay = new Date(date.getFullYear(),
                        date.getMonth(), daysInMonth(date.getMonth()+1,
                        date.getFullYear()));
                down.innerHTML = "First day = " + firstDay +
                            "<br>Last day = " + lastDay;
            }
		</script>
	</body>
</html>					
Output:
First day = Thu Jul 01 2021 00:00:00 GMT+0530 (India Standard Time)
Last day = Sat Jul 31 2021 00:00:00 GMT+0530 (India Standard Time)

It will help you....

Share: