How To Get Current Week Number in JavaScript?


Share:
Published By: Admin, Published On: Sep 27, 2021, Category: Javascript


Hello Friends,

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

Here i will give you many example how you can check javascript get current week number. Example : 1
<!DOCTYPE html>
<html>
		<head>
				<meta charset="utf-8">
				<title>JavaScript Get Current Week Number - MyWebtuts.com </title>
				<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
				<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
		</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">
												</div>
												<div class="card-body">
														<label>Enter date  YYYY-MM-DD :</label>
														<input id="cmd" value="2021-05-27" class="form-control">
														<button onclick="weekcheck(this)" class="btn btn-primary my-3">Check week number</button><br>
														<label>Moment:</label> 
														<input id="mWeek" class="form-control" readonly><br>
														<label>Answer:</label> 
														<input id="aWeek" class="form-control" readonly>
												</div>
										</div>
								</div>
						</div>
				</div>
				<script type="text/javascript">
						Date.prototype.getWeekNumber = function(){
						  	var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
						  	var dayNum = d.getUTCDay() || 7;
						  	d.setUTCDate(d.getUTCDate() + 4 - dayNum);
						  	var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
						  	return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
						};
						function weekcheck() {
						  	var s = document.getElementById('cmd').value;
						  	var m = moment(s, 'YYYY-MM-DD');
						  	document.getElementById('mWeek').value = m.format('W');
						  	document.getElementById('aWeek').value = m.toDate().getWeekNumber();      
						}
				</script>
		</body>
</html>
Output :
Enter date YYYY-MM-DD :
2021-05-27
Answer:
21
Example : 2
<!DOCTYPE html>
<html>
		<head>
				<meta charset="utf-8">
				<title>JavaScript Get Current Week Number - MyWebtuts.com </title>
		</head>
		<body>
				<script type="text/javascript">
						function getWeek(d) {
						    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
						   
						    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
						    var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
						    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
						    return [d.getUTCFullYear(), weekNo];
						}
						var result = getWeek(new Date());
						document.write('It\'s currently week ' + result[1] + ' of ' + result[0]);
				</script>
		</body>
</html>
Output :
It's currently week 30 of 2021

It will help you....

Share: