Package: effect
Module: Match
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"
See
instanceOf for matching instances of any constructorSignature
declare const date: Predicate.Refinement<unknown, Date>
Since v4.0.0