Skip to content

Instantly share code, notes, and snippets.

@IamSilviu
Last active January 24, 2022 08:40
Show Gist options
  • Save IamSilviu/5899269 to your computer and use it in GitHub Desktop.
Save IamSilviu/5899269 to your computer and use it in GitHub Desktop.
JavaScript Get week number.
Date.prototype.getWeek = function () {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
};
var myDate = new Date("2001-02-02");
myDate.getWeek(); //=> 5
@IamSilviu
Copy link
Author

86400000 = 1 day

@arthurPuszynski
Copy link

Nice. But if date includes non-zero hours or minutes, result is incorrect. This helps:

var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((new Date(this.getFullYear(), this.getMonth(), this.getDate()) - onejan) / 86400000) + onejan.getDay() + 1) / 7);

@TArch64
Copy link

TArch64 commented Nov 30, 2018

The same function with small refactoring:

function getNumberOfWeek() {
    const today = new Date();
    const firstDayOfYear = new Date(today.getFullYear(), 0, 1);
    const pastDaysOfYear = (today - firstDayOfYear) / 86400000;
    return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}

@sardar786786
Copy link

how to find week number in a month in javascript

@sardar786786
Copy link

how to find week number in a month in javascript

@mattisebastian
Copy link

The same function with small refactoring:

function getNumberOfWeek() {
    const today = new Date();
    const firstDayOfYear = new Date(today.getFullYear(), 0, 1);
    const pastDaysOfYear = (today - firstDayOfYear) / 86400000;
    return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}

works nicely, thanks!

@ayazzali
Copy link

ayazzali commented Sep 23, 2019

The same function with small refactoring:

function getNumberOfWeek() {
    const today = new Date();
    const firstDayOfYear = new Date(today.getFullYear(), 0, 1);
    const pastDaysOfYear = (today - firstDayOfYear) / 86400000;
    return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}

Sorry, but Its not always coorrect. for ex i found another func. see
( https://www.w3resource.com/javascript-exercises/javascript-date-exercise-24.php )

function getNumberOfWeek(date) {
    const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
    const pastDaysOfYear = (date - firstDayOfYear) / 86400000;
    return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}

function ISO8601_week_no(dt) {
    var tdt = new Date(dt.valueOf());
    var dayn = (dt.getDay() + 6) % 7;
    tdt.setDate(tdt.getDate() - dayn + 3);
    var firstThursday = tdt.valueOf();
    tdt.setMonth(0, 1);
    if (tdt.getDay() !== 4) {
        tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7);
    }
    return 1 + Math.ceil((firstThursday - tdt) / 604800000);
}

console.log('=============================');
dt = new Date(2015, 10, 1);
{
    let time = performance.now();

    console.log(ISO8601_week_no(dt)); //44

    time = performance.now() - time;
    console.log('Время выполнения1 = ', time);
}
{
    let time = performance.now();

    console.log(getNumberOfWeek(dt)); // 45

    time = performance.now() - time;
    console.log('Время выполнения12 = ', time);
}

@highviewstudios
Copy link

function getNumberOfWeek() {
    const today = new Date();
    const firstDayOfYear = new Date(today.getFullYear(), 0, 1);
    const pastDaysOfYear = (today - firstDayOfYear) / 86400000;
    return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}

Thanks for this 👍

@metehansenol
Copy link

Thanks for the function.

There is just small issue that typescript don't validate arithmetic operations on Date type so shows below error.

The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type

So I've called .valueOf() on Dates in that arithmetic operation.

getNumberOfWeek(): number {
    const today = new Date();
    const firstDayOfYear = new Date(today.getFullYear(), 0, 1);
    const pastDaysOfYear = (today.valueOf() - firstDayOfYear.valueOf()) / 86400000;
    return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7);
}

@kazoompa
Copy link

If you want the ISO solution there is a formula here that gives you the # of weeks of a given year:

https://en.wikipedia.org/wiki/ISO_week_date

@AusanSingh
Copy link

What will be week for Date ...

2022-01-01, //getDay() returning 6
2022-01-02, //getDay() returning 0
2022-01-03 //getDay() returning 1

As per above method

2022-01-01 is returning week 2 and next 6 dates is part of same week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment