Hyperlinkv0.8.0-beta.28

Match

Match.dateconsteffect/Match.ts:1632
(a: unknown): a is Date

Matches values that are instances of Date.

When to use

Use to match Date instances.

Details

This predicate refines unknown values to Date instances, allowing pattern matching on Date objects. It only matches actual Date instances, not date strings or timestamps.

Example (Matching Date instances)

import { Match } from "effect"

const processDateValue = Match.type<unknown>().pipe(
  Match.when(Match.date, (date) => {
    if (isNaN(date.getTime())) {
      return "Invalid date"
    }
    return `Date: ${date.toISOString().split("T")[0]}`
  }),
  Match.when(Match.string, (str) => `Date string: ${str}`),
  Match.orElse(() => "Not a date-related value")
)

console.log(processDateValue(new Date("2024-01-01"))) // "Date: 2024-01-01"
console.log(processDateValue(new Date("invalid"))) // "Invalid date"
console.log(processDateValue("2024-01-01")) // "Date string: 2024-01-01"
console.log(processDateValue(1704067200000)) // "Not a date-related value"
predicatesinstanceOf
Source effect/Match.ts:16321 lines
export const date: Predicate.Refinement<unknown, Date> = Predicate.isDate