TourKit
@tour-kit/schedulingUtilities

Day of Week Utilities

Day-of-week utilities: filter schedules by weekday with locale-aware day names and configurable week start day support

Day of Week Utilities

Utilities for checking and converting days of the week in a timezone-aware manner.

getDayOfWeek

Get the day of week for a date in a timezone.

import { getDayOfWeek } from '@tour-kit/scheduling'

const day = getDayOfWeek(new Date(), 'America/New_York')
// 0-6 (0 = Sunday, 6 = Saturday)

Returns the day number where 0 = Sunday, 6 = Saturday.

isAllowedDay

Check if a date falls on an allowed day.

import { isAllowedDay } from '@tour-kit/scheduling'

const weekdays = [1, 2, 3, 4, 5] // Mon-Fri

const isAllowed = isAllowedDay(new Date(), weekdays, 'UTC')
// true if Monday-Friday

dayNameToNumber

Convert a day name to a number.

import { dayNameToNumber } from '@tour-kit/scheduling'

dayNameToNumber('monday') // 1
dayNameToNumber('sunday') // 0
dayNameToNumber('saturday') // 6

dayNumberToName

Convert a day number to a name.

import { dayNumberToName } from '@tour-kit/scheduling'

dayNumberToName(0) // 'sunday'
dayNumberToName(1) // 'monday'
dayNumberToName(6) // 'saturday'

DAY_GROUPS

Pre-defined day groups.

import { DAY_GROUPS } from '@tour-kit/scheduling'

DAY_GROUPS.weekdays // [1, 2, 3, 4, 5]
DAY_GROUPS.weekends // [0, 6]
DAY_GROUPS.all // [0, 1, 2, 3, 4, 5, 6]

Examples

Weekdays Only

import { isAllowedDay, DAY_GROUPS } from '@tour-kit/scheduling'

function isWeekday(date: Date = new Date()): boolean {
  return isAllowedDay(date, DAY_GROUPS.weekdays, 'UTC')
}

if (isWeekday()) {
  showWorkdayFeature()
}

Timezone Aware

import { getDayOfWeek } from '@tour-kit/scheduling'

const date = new Date('2024-01-01T02:00:00Z') // 2am UTC on Monday

// In UTC - Monday (1)
getDayOfWeek(date, 'UTC')
// 1

// In Los Angeles (Sunday 6pm) - Sunday (0)
getDayOfWeek(date, 'America/Los_Angeles')
// 0

Weekend Detection

import { isAllowedDay, DAY_GROUPS } from '@tour-kit/scheduling'

function isWeekend(date: Date = new Date()): boolean {
  return isAllowedDay(date, DAY_GROUPS.weekends, 'UTC')
}

if (isWeekend()) {
  applyWeekendPricing()
}

Custom Day Groups

import { isAllowedDay } from '@tour-kit/scheduling'

// Only show tours on Tuesday and Thursday
const tutorialDays = [2, 4] // Tue, Thu

if (isAllowedDay(new Date(), tutorialDays, 'UTC')) {
  showWeeklyTutorial()
}

Convert User Input

import { dayNameToNumber } from '@tour-kit/scheduling'

function createScheduleFromInput(dayNames: string[]) {
  const daysOfWeek = dayNames.map(dayNameToNumber)

  return {
    daysOfWeek,
    timeOfDay: { start: '09:00', end: '17:00' },
  }
}

const schedule = createScheduleFromInput(['monday', 'wednesday', 'friday'])
// { daysOfWeek: [1, 3, 5], timeOfDay: { start: '09:00', end: '17:00' } }

Display Current Day

import { getDayOfWeek, dayNumberToName } from '@tour-kit/scheduling'

function getCurrentDayName(): string {
  const day = getDayOfWeek(new Date(), 'UTC')
  return dayNumberToName(day)
}

console.log(`Today is ${getCurrentDayName()}`)
// "Today is monday"

Filter by Multiple Day Types

import { isAllowedDay } from '@tour-kit/scheduling'

function shouldShowFeature(date: Date): boolean {
  // Show on weekdays OR the first day of month
  const isWeekday = isAllowedDay(date, [1, 2, 3, 4, 5], 'UTC')
  const isFirstOfMonth = date.getUTCDate() === 1

  return isWeekday || isFirstOfMonth
}

Build Day Selector UI

import { DAY_NAMES, dayNameToNumber } from '@tour-kit/scheduling'

function DaySelector({ value, onChange }: Props) {
  return (
    <div>
      {DAY_NAMES.map((dayName) => {
        const dayNum = dayNameToNumber(dayName)
        const isSelected = value.includes(dayNum)

        return (
          <button
            key={dayName}
            onClick={() => {
              const next = isSelected
                ? value.filter((d) => d !== dayNum)
                : [...value, dayNum]
              onChange(next)
            }}
          >
            {dayName}
          </button>
        )
      })}
    </div>
  )
}

Day Number Reference

NumberName
0Sunday
1Monday
2Tuesday
3Wednesday
4Thursday
5Friday
6Saturday

This matches JavaScript's Date.getDay() convention.

On this page