Skip to main content

Various JavaScript Date helper utilities from the is.js library.

//
// Date helper functions
// From the 'is.js' library: https://github.com/arasatasaygin/is.js
//
// https://jonlabelle.com/snippets/view/javascript/javascript-date-helper-functions
//

function isDate(value) {
    return Object.prototype.toString.call(value) === "[object Date]";
}

function isInDateRange(date, start, end) {
    if (!isDate(date) || !isDate(start) || !isDate(end)) {
        return false;
    }

    var stamp = date.getTime();

    return stamp > start.getTime() && stamp < end.getTime();
}

function isToday(date) {
    var now = new Date();
    var todayString = now.toDateString();

    return isDate(date) && date.toDateString() === todayString;
}

function isPast(date) {
    var now = new Date();
    return isDate(date) && date.getTime() < now.getTime();
}

function isTomorrow(date) {
    var now = new Date();

    var tomorrowString = new Date(
        now.setDate(now.getDate() + 1)
    ).toDateString();

    return isDate(date) && date.toDateString() === tomorrowString;
}

function isYesterday(date) {
    var now = new Date();

    var yesterdayString = new Date(
        now.setDate(now.getDate() - 1)
    ).toDateString();

    return isDate(date) && date.toDateString() === yesterdayString;
}

// is a given date weekend?
// 6: Saturday, 0: Sunday
function isWeekend(date) {
    return isDate(date) && (date.getDay() === 6 || date.getDay() === 0);
}

function isWeekday(date) {
    return !isWeekend(date);
}

function isDayLightSavingTime(date) {
    var january = new Date(date.getFullYear(), 0, 1);
    var july = new Date(date.getFullYear(), 6, 1);

    var stdTimezoneOffset = Math.max(
        january.getTimezoneOffset(),
        july.getTimezoneOffset()
    );

    return date.getTimezoneOffset() < stdTimezoneOffset;
}

function isQuarterOfYear(date, quarter) {
    return (
        isDate(date) &&
        Object.prototype.toString.call(quarter) === "[object Number]" &&
        quarter === Math.floor((date.getMonth() + 3) / 3)
    );
}

function isInLastMonth(date) {
    return isInDateRange(
        date,
        new Date(new Date().setMonth(new Date().getMonth() - 1)),
        new Date()
    );
}

// is a given date in last week range?
function inLastWeek(date) {
    return isInDateRange(
        date,
        new Date(new Date().setDate(new Date().getDate() - 7)),
        new Date()
    );
}

// is a given date in last year range?
function inLastYear(date) {
    return isInDateRange(
        date,
        new Date(new Date().setFullYear(new Date().getFullYear() - 1)),
        new Date()
    );
}

// is a given date in next month range?
function isInNextMonth(date) {
    return isInDateRange(
        date,
        new Date(),
        new Date(new Date().setMonth(new Date().getMonth() + 1))
    );
}

// is a given date in next week range?
function isInNextWeek(date) {
    return isInDateRange(
        date,
        new Date(),
        new Date(new Date().setDate(new Date().getDate() + 7))
    );
}

// is the given year a leap year?
function isLeapYear(year) {
    return (
        Object.prototype.toString.call(year) === "[object Number]" &&
        ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0)
    );
}