Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isWeekend

Checks if a given date falls on a weekend (Saturday or Sunday).

TypeScript
Copied!
1/**
2 * Checks if a given date falls on a weekend (Saturday or Sunday).
3 *
4 * @param date - The date to check.
5 * @returns True if the date is Saturday or Sunday, false otherwise.
6 */
7export function isWeekend(date: Date): boolean {
8  const day = date.getDay();
9
10  return day === 0 || day === 6;
11}
  • Simple and Fast Logic

    Uses a minimal and efficient check on the day index, making it extremely lightweight for frequent calls.

  • Built-in Method Compatibility

    Leverages Date.prototype.getDay() which is universally supported and reliable.

  • Zero Dependencies

    Requires no third-party utilities or polyfills — fully native and portable.

  • Clear and Readable

    Straightforward implementation that is immediately understandable and easy to maintain.

Tests | Examples

TypeScript
Copied!
1test('isWeekend - Saturday returns true', () => {
2  expect(isWeekend(new Date('2025-06-28'))).toBe(true); // Saturday
3});
4
5test('isWeekend - Sunday returns true', () => {
6  expect(isWeekend(new Date('2025-06-29'))).toBe(true); // Sunday
7});
8
9test('isWeekend - Monday returns false', () => {
10  expect(isWeekend(new Date('2025-06-30'))).toBe(false); // Monday
11});
12
13test('isWeekend - Friday returns false', () => {
14  expect(isWeekend(new Date('2025-06-27'))).toBe(false); // Friday
15});

Common Use Cases

  • Business Logic Filters

    Exclude weekends when scheduling meetings, deliveries, or deadlines.

  • UI Calendar Highlighting

    Mark weekend days differently in date pickers or calendar views.

  • Workday Validation

    Validate user input or automate restrictions to allow selection of weekdays only.

  • Logging and Analytics

    Flag events that occur during weekends for behavioral analysis or exception handling.

Codebase: Utilities -> Dates -> isWeekend | Yevhen Klymentiev