Open In App

Calculate current week number in JavaScript

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Calculating the current week number involves determining which week of the year the current date falls into. This is often based on a system where weeks start on a specific day, typically Sunday or Monday.

Example:

The week number of january 1  = either week 52 or week 1

Explanation: From the below calendar we can see that Jan 1 is in last week52 of the previous year. If it was Monday, then Jan1 would be week 1

Approach:

  • Date Initialization: Ensure currentDate is set to either the provided date object or the current date using new Date().
  • Calculate January 1st: Initialize januaryFirst to the beginning of the current year using new Date(currentDate.getFullYear(), 0, 1).
  • Days to Next Monday: Determine the number of days to the next Monday from January 1st using conditional logic.
  • Calculate Next Monday: Set nextMonday to the date of the next Monday following January 1st.
  • Return Week Number: Compare currentDate with nextMonday and calculate the week number accordingly, returning the result.

Example: In this example, we are calculating the total number of weeks till now.

Javascript




function getDateWeek(date) {
    const currentDate =
        (typeof date === 'object') ? date : new Date();
    const januaryFirst =
        new Date(currentDate.getFullYear(), 0, 1);
    const daysToNextMonday =
        (januaryFirst.getDay() === 1) ? 0 :
        (7 - januaryFirst.getDay()) % 7;
    const nextMonday =
        new Date(currentDate.getFullYear(), 0,
        januaryFirst.getDate() + daysToNextMonday);
 
    return (currentDate < nextMonday) ? 52 :
    (currentDate > nextMonday ? Math.ceil(
    (currentDate - nextMonday) / (24 * 3600 * 1000) / 7) : 1);
}
 
const currentDate = new Date();
const weekNumber = getDateWeek();
 
console.log("Week number of " + currentDate + " is : " + weekNumber);


Output

Week number of Mon Mar 11 2024 04:57:07 GMT+0000 (Coordinated Universal Time) is : 11

the


Previous Article
Next Article

Similar Reads

Moment.js Customize First Day of Week and First Week of Year
Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. In the calendar, we can use Moment.js to customize the First Day of the Week and the First Week of the Year as per our requirements. We will be using the moment.updateLocale() method for this customization. Syntax: moment.updateLocale('en', { week :
2 min read
How to Get Last Week's Date using JavaScript ?
In this article, we are going to learn about how to get last week's Date by using JavaScriptthe. JavaScript's dynamic nature improves websites by adding dynamic elements. Fetching the previous week's date is vital for event schedules, calendars, and timely data display. There are several methods that can be used to get the last week's Date using Ja
4 min read
PHP | Number of week days between two dates
You are given two string (dd-mm-yyyy) representing two date, you have to find number of all weekdays present in between given dates.(both inclusive) Examples: Input : startDate = "01-01-2018" endDate = "01-03-2018" Output : Array ( [Monday] =&gt; 9 [Tuesday] =&gt; 9 [Wednesday] =&gt; 9 [Thursday] =&gt; 9 [Friday] =&gt; 8 [Saturday] =&gt; 8 [Sunday]
2 min read
Print current day and time using HTML and JavaScript
The task is to print the system's current day and time in the following format(Time in hours, minutes and milliseconds). Approach: Use getDay() function to create an array of days in a week. It returns a number of day like 0 for Sunday, 1 for Monday and so on). Convert it into AM or PM using ternary operator. Use getHours() method to get the hour v
2 min read
How to get the current date in JavaScript ?
In this article, we will see how to get the current date using the built-in methods in JavaScript &amp; will see their implementation through the examples. There are 2 methods to perform this task: Using Date().toDateString() MethodUsing their respective method for fetching day, month, and year.There are several other ways to perform this task but
3 min read
How to get the first and last date of current month using JavaScript ?
Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Parameters: This method does not accept any parameters. Return value: It returns a number from 1 to 31, r
3 min read
How to format current date in MM/DD/YYYY HH:MM:SS format using JavaScript ?
Given a date and the task is to format the current date in MM/DD/YYYY HH:MM:SS format. Here are a few of the most techniques discussed with the help of JavaScript. Approach 1: Store the current date in variable.Use the string concatenation technique to insert / and : in between the month-day and day-year.Use .slice() method to format the day, month
2 min read
How to list all the cookies of the current page using JavaScript ?
In this article, we will learn how to get the list of all the cookies for the current page in JavaScript, along with understanding their implementation through the examples. The task is to retrieve the cookies stored in the current domain (We can not get all cookies due to security reasons). There are two methods to solve this problem which are dis
2 min read
How to print the content of current window using JavaScript ?
The task is to print the content of the current window by using the window.print() method in the document. It is used to open the Print Dialog Box to print the current document. It does not have any parameter value. Syntax: window.print() Parameters: No parameters are required Returns: This function does not return anything Example: This example re
1 min read
How to get the current URL using JavaScript ?
In this article, we will know about getting the Website URL for the current webpage or website. The current URL can be obtained by using the 'URL' property of the Document object which contains information about the current URL. The 'URL' property returns a string with the full location of the current page, along with containing the string having t
2 min read