How to Get Week Number of the Year in JavaScript

Subodh Poudel Feb 02, 2024
  1. Use the getFullYear() and getDay() Functions Along With the new Date()’s Object to Get the Week Number of the Current Date
  2. Use the DateTime Data Structure and Its weekNumber Properties From Luxon to Get the Current Week Number of the Year
How to Get Week Number of the Year in JavaScript

We will introduce a method to find the current week number of the year in JavaScript, creating the object of the new Date() constructor and the functions like getFullYear() and getDay(). This method will find the total number of days of the year and find the week number. We will use the Math object’s functions like floor() and ceil() in this method.

This article will also introduce another method of getting the current week number in JavaScript using a javascript wrapper Luxon. Luxon is a library of JavaScript dates and times. We will use the DateTime data structure and the weekNumber property from this library. We will also use the now function with the DataTime data structure.

We will also demonstrate a method to declare a global variable in PHP using the define() function. This method is a way of declaring a constant global variable in PHP. The value of the variable cannot be changed later.

Use the getFullYear() and getDay() Functions Along With the new Date()’s Object to Get the Week Number of the Current Date

This method uses the new Date() constructor and its object along with the functions like getFullYear() and getDay() to get the current week number of the year. The Date object returns the current date. The getDay() function finds the current day of the week in an integer value. We can create an object of the Date() constructor to get the current date. The object calls the function getFullYear() to get the start of the current year. We can find the total number of days from the start of the day to the current time by dividing the days’ difference by the total milliseconds in a day. We can use the floor() function of the Math object to round off the value to get a whole number. This method finally calculates the week number of the year, dividing the total number of the days by seven.

For example, create a Date object currentdate. Create another variable, oneJan, to store the first day of the current year. For that, create a new Date object on the variable and call the getFullYear() function with the currentdate object as the first parameter of the Date() constructor. Use 0 and 1 as the second and the third parameter. Subtract the variable oneJan from currentdate and divide it by 86400000. Wrap the operations inside Math.floor() function and assign it to a variable numberOfDays. Call the getDay() function with the currentdate object and add the variable numberOfDays and the value 1 to it. Wrap this operation with the Math.ceil() function and store the result in the result variable. Log the variable in the console along with the currentdate variable using string interpolation.

The example below finds the current date and then the first day of the current year. The parameters 0 and 1 in the second line represents the first month and the first day of the current year. Subtracting the oneJan from the currentdate gives the value in milliseconds, so the total milliseconds in a day which is 86400000 divides the difference. The getDay() function returns the day in integer form starting with 0, so we add 1. Therefore, the current week number is calculated.

Example Code:

currentdate = new Date();
var oneJan = new Date(currentdate.getFullYear(), 0, 1);
var numberOfDays = Math.floor((currentdate - oneJan) / (24 * 60 * 60 * 1000));
var result = Math.ceil((currentdate.getDay() + 1 + numberOfDays) / 7);
console.log(
    `The week number of the current date (${currentdate}) is ${result}.`);

Output:

The week number of the current date (Tue May 25 2021 16:55:53 GMT+0545 (Nepal Time)) is 21.

Use the DateTime Data Structure and Its weekNumber Properties From Luxon to Get the Current Week Number of the Year

We can use the Luxon library to get the current week number of the year with the DateTime data structure and weeknumber property. The DateTime consists of a timestamp, a time zone, and configuration properties. The DateTime used with the now function will return the date and time for the current instant in the system’s timezone. The weekNumber property accesses the week number of the current year. We can use the toISO() function to convert the DateTime to the ISO format.

Import the DateTime object from the luxon/src/datetime.js in the first line. Do not forget to download the Luxon source code. Save the source code in a JavaScript file and make sure to include the file using the script tag in the HTML section. Call the now() function with the DateTime object and then call the weekNumber property. Assign it to a variable date. Then, log the variable date in the console. Please consult the Luxon Documentation to know more about the DateTime objects and properties.

Code Example:

import DateTime from 'luxon/src/datetime.js'
const date = DateTime.now().weekNumber
console.log(`The current week number is ${date}`)

Output:

The current week number is 21
Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn

Related Article - JavaScript DateTime