DEV Community

Cover image for Two Dates in The Same Week
Matt Ellen-Tsivintzeli
Matt Ellen-Tsivintzeli

Posted on • Originally published at gist.github.com

4 2

Two Dates in The Same Week

Have you ever wondered if two dates are in the same week? I have. So I wrote this function to detect such an event.

Enjoy!

function areSameWeeks(date1, date2, boundaryDay=0)
{
    if(date1 > date2)
    {
        let t = date1;
        date1 = date2;
        date2 = t;
    }

    if(((((date2 - date1)/1000)/3600)/24)>6)
    {
        return false;
    }

    let day1 = date1.getUTCDay();
    let day2 = date2.getUTCDay();

    if(day1 == boundaryDay)
    {
        return true;
    }

    if(day2 == boundaryDay)
    {
        return false;
    }

    let d1BoundaryDist = ((day1-boundaryDay)+7)%7;
    let d2BoundaryDist = ((day2-boundaryDay)+7)%7;

    if(d1BoundaryDist <= d2BoundaryDist)
    {
        return true;
    }

    return false;
}
Enter fullscreen mode Exit fullscreen mode

Billboard image

Monitor more than uptime.

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay