Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get weeks between two dates,

  1. Subtract the date to get the difference in nanoseconds.
  2. Devied your output time with / 1000 * 3600 * 24 * 7

It will return your difference in dates in a number of weeks.

Today, I’m going to show How do I get weeks between two dates in javascript, here I will use the javascript getTime() method and the above-mentioned conditions to get weeks between the start and end date time.

Let’s start the today’s tutorial How do you get weeks between two dates in javascript?

In the following example, we are going to do

  1. create two dates starting and ending date
  2. create a function that accepts a date and returns weeks
  3. Use a function for multiple date examples

let’s write the code.

function getWeeks(startingDate, endingDate) {
    return parseInt(Math.abs(endingDate.getTime() - startingDate.getTime()) / (1000 * 3600 * 24 * 7));

// 👇️ 530
console.log(getWeeks(new Date('2012-01-15'), new Date('2022-03-16')));

// 👇️ 439
console.log(getWeeks(new Date('2014-01-15'), new Date('2022-06-16')));

// 👇️ 8
console.log(getWeeks(new Date('2023-01-15'), new Date('2023-03-16')));

In the above program, we have created a custom function getWeeks() and pass dates to get a number of weeks.

let’s check the output.

javascript, get weeks between two dates example
javascript, get weeks between two dates example

I hope it’s help you, All the best 👍.