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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay