DateTime.ts overview
Since v3.6.0
Exports Grouped by Category
- comparisons
- constructors
- conversions
- current time zone
- formatting
- guards
- instances
- mapping
- math
- models
- parts
- time zones
- type ids
- utils
comparisons
between
Signature
declare const between: {
(options: { minimum: DateTime; maximum: DateTime }): (self: DateTime) => boolean
(self: DateTime, options: { minimum: DateTime; maximum: DateTime }): boolean
}
Since v3.6.0
distance
Calulate the difference between two DateTime
values, returning the number of milliseconds the other
DateTime is from self
.
If other
is after self
, the result will be a positive number.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
const other = DateTime.add(now, { minutes: 1 })
// returns 60000
DateTime.distance(now, other)
})
Signature
declare const distance: { (other: DateTime): (self: DateTime) => number; (self: DateTime, other: DateTime): number }
Since v3.6.0
distanceDuration
Calulate the distance between two DateTime
values.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
const other = DateTime.add(now, { minutes: 1 })
// returns Duration.minutes(1)
DateTime.distanceDuration(now, other)
})
Signature
declare const distanceDuration: {
(other: DateTime): (self: DateTime) => Duration.Duration
(self: DateTime, other: DateTime): Duration.Duration
}
Since v3.6.0
distanceDurationEither
Calulate the difference between two DateTime
values.
If the other
DateTime is before self
, the result will be a negative Duration
, returned as a Left
.
If the other
DateTime is after self
, the result will be a positive Duration
, returned as a Right
.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
const other = DateTime.add(now, { minutes: 1 })
// returns Either.right(Duration.minutes(1))
DateTime.distanceDurationEither(now, other)
// returns Either.left(Duration.minutes(1))
DateTime.distanceDurationEither(other, now)
})
Signature
declare const distanceDurationEither: {
(other: DateTime): (self: DateTime) => Either.Either<Duration.Duration, Duration.Duration>
(self: DateTime, other: DateTime): Either.Either<Duration.Duration, Duration.Duration>
}
Since v3.6.0
greaterThan
Signature
declare const greaterThan: { (that: DateTime): (self: DateTime) => boolean; (self: DateTime, that: DateTime): boolean }
Since v3.6.0
greaterThanOrEqualTo
Signature
declare const greaterThanOrEqualTo: {
(that: DateTime): (self: DateTime) => boolean
(self: DateTime, that: DateTime): boolean
}
Since v3.6.0
isFuture
Signature
declare const isFuture: (self: DateTime) => Effect.Effect<boolean>
Since v3.6.0
isPast
Signature
declare const isPast: (self: DateTime) => Effect.Effect<boolean>
Since v3.6.0
lessThan
Signature
declare const lessThan: { (that: DateTime): (self: DateTime) => boolean; (self: DateTime, that: DateTime): boolean }
Since v3.6.0
lessThanOrEqualTo
Signature
declare const lessThanOrEqualTo: {
(that: DateTime): (self: DateTime) => boolean
(self: DateTime, that: DateTime): boolean
}
Since v3.6.0
max
Signature
declare const max: {
<That extends DateTime>(that: That): <Self extends DateTime>(self: Self) => Self | That
<Self extends DateTime, That extends DateTime>(self: Self, that: That): Self | That
}
Since v3.6.0
min
Signature
declare const min: {
<That extends DateTime>(that: That): <Self extends DateTime>(self: Self) => Self | That
<Self extends DateTime, That extends DateTime>(self: Self, that: That): Self | That
}
Since v3.6.0
unsafeIsFuture
Signature
declare const unsafeIsFuture: (self: DateTime) => boolean
Since v3.6.0
unsafeIsPast
Signature
declare const unsafeIsPast: (self: DateTime) => boolean
Since v3.6.0
constructors
make
Create a DateTime
from one of the following:
- A
DateTime
- A
Date
instance (invalid dates will throw anIllegalArgumentException
) - The
number
of milliseconds since the Unix epoch - An object with the parts of a date
- A
string
that can be parsed byDate.parse
If the input is invalid, None
will be returned.
Example
import { DateTime } from "effect"
// from Date
DateTime.make(new Date())
// from parts
DateTime.make({ year: 2024 })
// from string
DateTime.make("2024-01-01")
Signature
declare const make: <A extends DateTime.Input>(input: A) => Option.Option<DateTime.PreserveZone<A>>
Since v3.6.0
makeZoned
Create a DateTime.Zoned
using DateTime.make
and a time zone.
The input is treated as UTC and then the time zone is attached.
If the date time input or time zone is invalid, None
will be returned.
Example
import { DateTime } from "effect"
DateTime.makeZoned(new Date(), { timeZone: "Europe/London" })
Signature
declare const makeZoned: (
input: DateTime.Input,
options?: {
readonly timeZone?: number | string | TimeZone | undefined
readonly adjustForTimeZone?: boolean | undefined
}
) => Option.Option<Zoned>
Since v3.6.0
makeZonedFromString
Create a DateTime.Zoned
from a string.
It uses the format: YYYY-MM-DDTHH:mm:ss.sss+HH:MM[Time/Zone]
.
Signature
declare const makeZonedFromString: (input: string) => Option.Option<Zoned>
Since v3.6.0
now
Get the current time using the Clock
service and convert it to a DateTime
.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
})
Signature
declare const now: Effect.Effect<Utc, never, never>
Since v3.6.0
nowAsDate
Get the current time using the Clock
service.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.nowAsDate
})
Signature
declare const nowAsDate: Effect.Effect<Date, never, never>
Since v3.14.0
unsafeFromDate
Create a DateTime
from a Date
.
If the Date
is invalid, an IllegalArgumentException
will be thrown.
Signature
declare const unsafeFromDate: (date: Date) => Utc
Since v3.6.0
unsafeMake
Create a DateTime
from one of the following:
- A
DateTime
- A
Date
instance (invalid dates will throw anIllegalArgumentException
) - The
number
of milliseconds since the Unix epoch - An object with the parts of a date
- A
string
that can be parsed byDate.parse
Example
import { DateTime } from "effect"
// from Date
DateTime.unsafeMake(new Date())
// from parts
DateTime.unsafeMake({ year: 2024 })
// from string
DateTime.unsafeMake("2024-01-01")
Signature
declare const unsafeMake: <A extends DateTime.Input>(input: A) => DateTime.PreserveZone<A>
Since v3.6.0
unsafeMakeZoned
Create a DateTime.Zoned
using DateTime.unsafeMake
and a time zone.
The input is treated as UTC and then the time zone is attached, unless adjustForTimeZone
is set to true
. In that case, the input is treated as already in the time zone.
Example
import { DateTime } from "effect"
DateTime.unsafeMakeZoned(new Date(), { timeZone: "Europe/London" })
Signature
declare const unsafeMakeZoned: (
input: DateTime.Input,
options?: {
readonly timeZone?: number | string | TimeZone | undefined
readonly adjustForTimeZone?: boolean | undefined
}
) => Zoned
Since v3.6.0
unsafeNow
Get the current time using Date.now
.
Signature
declare const unsafeNow: LazyArg<Utc>
Since v3.6.0
conversions
removeTime
Remove the time aspect of a DateTime
, first adjusting for the time zone. It will return a DateTime.Utc
only containing the date.
Example
import { DateTime } from "effect"
// returns "2024-01-01T00:00:00Z"
DateTime.unsafeMakeZoned("2024-01-01T05:00:00Z", {
timeZone: "Pacific/Auckland",
adjustForTimeZone: true
}).pipe(DateTime.removeTime, DateTime.formatIso)
Signature
declare const removeTime: (self: DateTime) => Utc
Since v3.6.0
toDate
Convert a DateTime
to a Date
, applying the time zone first.
Signature
declare const toDate: (self: DateTime) => Date
Since v3.6.0
toDateUtc
Get the UTC Date
of a DateTime
.
Signature
declare const toDateUtc: (self: DateTime) => Date
Since v3.6.0
toEpochMillis
Get the milliseconds since the Unix epoch of a DateTime
.
Signature
declare const toEpochMillis: (self: DateTime) => number
Since v3.6.0
zonedOffset
Calculate the time zone offset of a DateTime.Zoned
in milliseconds.
Signature
declare const zonedOffset: (self: Zoned) => number
Since v3.6.0
zonedOffsetIso
Calculate the time zone offset of a DateTime
in milliseconds.
The offset is formatted as “±HH:MM”.
Signature
declare const zonedOffsetIso: (self: Zoned) => string
Since v3.6.0
current time zone
CurrentTimeZone (class)
Signature
declare class CurrentTimeZone
Since v3.11.0
layerCurrentZone
Create a Layer from the given time zone.
Signature
declare const layerCurrentZone: (zone: TimeZone) => Layer.Layer<CurrentTimeZone>
Since v3.6.0
layerCurrentZoneLocal
Create a Layer from the systems local time zone.
Signature
declare const layerCurrentZoneLocal: Layer.Layer<CurrentTimeZone, never, never>
Since v3.6.0
layerCurrentZoneNamed
Create a Layer from the given IANA time zone identifier.
Signature
declare const layerCurrentZoneNamed: (zoneId: string) => Layer.Layer<CurrentTimeZone, IllegalArgumentException>
Since v3.6.0
layerCurrentZoneOffset
Create a Layer from the given time zone offset.
Signature
declare const layerCurrentZoneOffset: (offset: number) => Layer.Layer<CurrentTimeZone>
Since v3.6.0
nowInCurrentZone
Get the current time as a DateTime.Zoned
, using the CurrentTimeZone
.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
// will use the "Europe/London" time zone
const now = yield* DateTime.nowInCurrentZone
}).pipe(DateTime.withCurrentZoneNamed("Europe/London"))
Signature
declare const nowInCurrentZone: Effect.Effect<Zoned, never, CurrentTimeZone>
Since v3.6.0
setZoneCurrent
Set the time zone of a DateTime
to the current time zone, which is determined by the CurrentTimeZone
service.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
// set the time zone to "Europe/London"
const zoned = yield* DateTime.setZoneCurrent(now)
}).pipe(DateTime.withCurrentZoneNamed("Europe/London"))
Signature
declare const setZoneCurrent: (self: DateTime) => Effect.Effect<Zoned, never, CurrentTimeZone>
Since v3.6.0
withCurrentZone
Provide the CurrentTimeZone
to an effect.
Example
import { DateTime, Effect } from "effect"
const zone = DateTime.zoneUnsafeMakeNamed("Europe/London")
Effect.gen(function* () {
const now = yield* DateTime.nowInCurrentZone
}).pipe(DateTime.withCurrentZone(zone))
Signature
declare const withCurrentZone: {
(zone: TimeZone): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, CurrentTimeZone>>
<A, E, R>(effect: Effect.Effect<A, E, R>, zone: TimeZone): Effect.Effect<A, E, Exclude<R, CurrentTimeZone>>
}
Since v3.6.0
withCurrentZoneLocal
Provide the CurrentTimeZone
to an effect, using the system’s local time zone.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
// will use the system's local time zone
const now = yield* DateTime.nowInCurrentZone
}).pipe(DateTime.withCurrentZoneLocal)
Signature
declare const withCurrentZoneLocal: <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, Exclude<R, CurrentTimeZone>>
Since v3.6.0
withCurrentZoneNamed
Provide the CurrentTimeZone
to an effect using an IANA time zone identifier.
If the time zone is invalid, it will fail with an IllegalArgumentException
.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
// will use the "Europe/London" time zone
const now = yield* DateTime.nowInCurrentZone
}).pipe(DateTime.withCurrentZoneNamed("Europe/London"))
Signature
declare const withCurrentZoneNamed: {
(
zone: string
): <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E | IllegalArgumentException, Exclude<R, CurrentTimeZone>>
<A, E, R>(
effect: Effect.Effect<A, E, R>,
zone: string
): Effect.Effect<A, E | IllegalArgumentException, Exclude<R, CurrentTimeZone>>
}
Since v3.6.0
withCurrentZoneOffset
Provide the CurrentTimeZone
to an effect, using a offset.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
// will use the system's local time zone
const now = yield* DateTime.nowInCurrentZone
}).pipe(DateTime.withCurrentZoneOffset(3 * 60 * 60 * 1000))
Signature
declare const withCurrentZoneOffset: {
(offset: number): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, CurrentTimeZone>>
<A, E, R>(effect: Effect.Effect<A, E, R>, offset: number): Effect.Effect<A, E, Exclude<R, CurrentTimeZone>>
}
Since v3.6.0
formatting
format
Format a DateTime
as a string using the DateTimeFormat
API.
The timeZone
option is set to the offset of the time zone.
Note: On Node versions < 22, fixed “Offset” zones will set the time zone to “UTC” and use the adjusted Date
.
Signature
declare const format: {
(
options?: (Intl.DateTimeFormatOptions & { readonly locale?: string | undefined }) | undefined
): (self: DateTime) => string
(
self: DateTime,
options?: (Intl.DateTimeFormatOptions & { readonly locale?: string | undefined }) | undefined
): string
}
Since v3.6.0
formatIntl
Format a DateTime
as a string using the DateTimeFormat
API.
Signature
declare const formatIntl: {
(format: Intl.DateTimeFormat): (self: DateTime) => string
(self: DateTime, format: Intl.DateTimeFormat): string
}
Since v3.6.0
formatIso
Format a DateTime
as a UTC ISO string.
Signature
declare const formatIso: (self: DateTime) => string
Since v3.6.0
formatIsoDate
Format a DateTime
as a time zone adjusted ISO date string.
Signature
declare const formatIsoDate: (self: DateTime) => string
Since v3.6.0
formatIsoDateUtc
Format a DateTime
as a UTC ISO date string.
Signature
declare const formatIsoDateUtc: (self: DateTime) => string
Since v3.6.0
formatIsoOffset
Format a DateTime.Zoned
as a ISO string with an offset.
Signature
declare const formatIsoOffset: (self: DateTime) => string
Since v3.6.0
formatIsoZoned
Format a DateTime.Zoned
as a string.
It uses the format: YYYY-MM-DDTHH:mm:ss.sss+HH:MM[Time/Zone]
.
Signature
declare const formatIsoZoned: (self: Zoned) => string
Since v3.6.0
formatLocal
Format a DateTime
as a string using the DateTimeFormat
API.
It will use the system’s local time zone & locale.
Signature
declare const formatLocal: {
(
options?: (Intl.DateTimeFormatOptions & { readonly locale?: string | undefined }) | undefined
): (self: DateTime) => string
(
self: DateTime,
options?: (Intl.DateTimeFormatOptions & { readonly locale?: string | undefined }) | undefined
): string
}
Since v3.6.0
formatUtc
Format a DateTime
as a string using the DateTimeFormat
API.
This forces the time zone to be UTC.
Signature
declare const formatUtc: {
(
options?: (Intl.DateTimeFormatOptions & { readonly locale?: string | undefined }) | undefined
): (self: DateTime) => string
(
self: DateTime,
options?: (Intl.DateTimeFormatOptions & { readonly locale?: string | undefined }) | undefined
): string
}
Since v3.6.0
guards
isDateTime
Signature
declare const isDateTime: (u: unknown) => u is DateTime
Since v3.6.0
isTimeZone
Signature
declare const isTimeZone: (u: unknown) => u is TimeZone
Since v3.6.0
isTimeZoneNamed
Signature
declare const isTimeZoneNamed: (u: unknown) => u is TimeZone.Named
Since v3.6.0
isTimeZoneOffset
Signature
declare const isTimeZoneOffset: (u: unknown) => u is TimeZone.Offset
Since v3.6.0
isUtc
Signature
declare const isUtc: (self: DateTime) => self is Utc
Since v3.6.0
isZoned
Signature
declare const isZoned: (self: DateTime) => self is Zoned
Since v3.6.0
instances
Equivalence
Signature
declare const Equivalence: equivalence.Equivalence<DateTime>
Since v3.6.0
Order
Signature
declare const Order: order.Order<DateTime>
Since v3.6.0
mapping
mapEpochMillis
Transform a DateTime
by applying a function to the number of milliseconds since the Unix epoch.
Example
import { DateTime } from "effect"
// add 10 milliseconds
DateTime.unsafeMake(0).pipe(DateTime.mapEpochMillis((millis) => millis + 10))
Signature
declare const mapEpochMillis: {
(f: (millis: number) => number): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, f: (millis: number) => number): A
}
Since v3.6.0
match
Signature
declare const match: {
<A, B>(options: { readonly onUtc: (_: Utc) => A; readonly onZoned: (_: Zoned) => B }): (self: DateTime) => A | B
<A, B>(self: DateTime, options: { readonly onUtc: (_: Utc) => A; readonly onZoned: (_: Zoned) => B }): A | B
}
Since v3.6.0
mutate
Modify a DateTime
by applying a function to a cloned Date
instance.
The Date
will first have the time zone applied if possible, and then be converted back to a DateTime
within the same time zone.
Signature
declare const mutate: {
(f: (date: Date) => void): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, f: (date: Date) => void): A
}
Since v3.6.0
mutateUtc
Modify a DateTime
by applying a function to a cloned UTC Date
instance.
Signature
declare const mutateUtc: {
(f: (date: Date) => void): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, f: (date: Date) => void): A
}
Since v3.6.0
withDate
Using the time zone adjusted Date
, apply a function to the Date
and return the result.
Example
import { DateTime } from "effect"
// get the time zone adjusted date in milliseconds
DateTime.unsafeMakeZoned(0, { timeZone: "Europe/London" }).pipe(DateTime.withDate((date) => date.getTime()))
Signature
declare const withDate: {
<A>(f: (date: Date) => A): (self: DateTime) => A
<A>(self: DateTime, f: (date: Date) => A): A
}
Since v3.6.0
withDateUtc
Using the time zone adjusted Date
, apply a function to the Date
and return the result.
Example
import { DateTime } from "effect"
// get the date in milliseconds
DateTime.unsafeMake(0).pipe(DateTime.withDateUtc((date) => date.getTime()))
Signature
declare const withDateUtc: {
<A>(f: (date: Date) => A): (self: DateTime) => A
<A>(self: DateTime, f: (date: Date) => A): A
}
Since v3.6.0
math
add
Add the given amount
of unit
’s to a DateTime
.
The time zone is taken into account when adding days, weeks, months, and years.
Example
import { DateTime } from "effect"
// add 5 minutes
DateTime.unsafeMake(0).pipe(DateTime.add({ minutes: 5 }))
Signature
declare const add: {
(parts: Partial<DateTime.PartsForMath>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsForMath>): A
}
Since v3.6.0
addDuration
Add the given Duration
to a DateTime
.
Example
import { DateTime } from "effect"
// add 5 minutes
DateTime.unsafeMake(0).pipe(DateTime.addDuration("5 minutes"))
Signature
declare const addDuration: {
(duration: Duration.DurationInput): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, duration: Duration.DurationInput): A
}
Since v3.6.0
endOf
Converts a DateTime
to the end of the given part
.
If the part is week
, the weekStartsOn
option can be used to specify the day of the week that the week starts on. The default is 0 (Sunday).
Example
import { DateTime } from "effect"
// returns "2024-01-01T23:59:59.999Z"
DateTime.unsafeMake("2024-01-01T12:00:00Z").pipe(DateTime.endOf("day"), DateTime.formatIso)
Signature
declare const endOf: {
(
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): <A extends DateTime>(self: A) => A
<A extends DateTime>(
self: A,
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): A
}
Since v3.6.0
nearest
Converts a DateTime
to the nearest given part
.
If the part is week
, the weekStartsOn
option can be used to specify the day of the week that the week starts on. The default is 0 (Sunday).
Example
import { DateTime } from "effect"
// returns "2024-01-02T00:00:00Z"
DateTime.unsafeMake("2024-01-01T12:01:00Z").pipe(DateTime.nearest("day"), DateTime.formatIso)
Signature
declare const nearest: {
(
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): <A extends DateTime>(self: A) => A
<A extends DateTime>(
self: A,
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): A
}
Since v3.6.0
startOf
Converts a DateTime
to the start of the given part
.
If the part is week
, the weekStartsOn
option can be used to specify the day of the week that the week starts on. The default is 0 (Sunday).
Example
import { DateTime } from "effect"
// returns "2024-01-01T00:00:00Z"
DateTime.unsafeMake("2024-01-01T12:00:00Z").pipe(DateTime.startOf("day"), DateTime.formatIso)
Signature
declare const startOf: {
(
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): <A extends DateTime>(self: A) => A
<A extends DateTime>(
self: A,
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): A
}
Since v3.6.0
subtract
Subtract the given amount
of unit
’s from a DateTime
.
Example
import { DateTime } from "effect"
// subtract 5 minutes
DateTime.unsafeMake(0).pipe(DateTime.subtract({ minutes: 5 }))
Signature
declare const subtract: {
(parts: Partial<DateTime.PartsForMath>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsForMath>): A
}
Since v3.6.0
subtractDuration
Subtract the given Duration
from a DateTime
.
Example
import { DateTime } from "effect"
// subtract 5 minutes
DateTime.unsafeMake(0).pipe(DateTime.subtractDuration("5 minutes"))
Signature
declare const subtractDuration: {
(duration: Duration.DurationInput): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, duration: Duration.DurationInput): A
}
Since v3.6.0
models
DateTime (type alias)
A DateTime
represents a point in time. It can optionally have a time zone associated with it.
Signature
type DateTime = Utc | Zoned
Since v3.6.0
DateTime (namespace)
Since v3.6.0
PartsWithWeekday (interface)
Signature
export interface PartsWithWeekday {
readonly millis: number
readonly seconds: number
readonly minutes: number
readonly hours: number
readonly day: number
readonly weekDay: number
readonly month: number
readonly year: number
}
Since v3.6.0
Parts (interface)
Signature
export interface Parts {
readonly millis: number
readonly seconds: number
readonly minutes: number
readonly hours: number
readonly day: number
readonly month: number
readonly year: number
}
Since v3.6.0
PartsForMath (interface)
Signature
export interface PartsForMath {
readonly millis: number
readonly seconds: number
readonly minutes: number
readonly hours: number
readonly days: number
readonly weeks: number
readonly months: number
readonly years: number
}
Since v3.6.0
Proto (interface)
Signature
export interface Proto extends Pipeable, Inspectable {
readonly [TypeId]: TypeId
}
Since v3.6.0
Input (type alias)
Signature
type Input = DateTime | Partial<Parts> | Date | number | string
Since v3.6.0
PreserveZone (type alias)
Signature
type PreserveZone<A> = A extends Zoned ? Zoned : Utc
Since v3.6.0
Unit (type alias)
Signature
type Unit = UnitSingular | UnitPlural
Since v3.6.0
UnitSingular (type alias)
Signature
type UnitSingular = "milli" | "second" | "minute" | "hour" | "day" | "week" | "month" | "year"
Since v3.6.0
UnitPlural (type alias)
Signature
type UnitPlural = "millis" | "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years"
Since v3.6.0
TimeZone (type alias)
Signature
type TimeZone = TimeZone.Offset | TimeZone.Named
Since v3.6.0
TimeZone (namespace)
Since v3.6.0
Proto (interface)
Signature
export interface Proto extends Inspectable {
readonly [TimeZoneTypeId]: TimeZoneTypeId
}
Since v3.6.0
Offset (interface)
Signature
export interface Offset extends Proto {
readonly _tag: "Offset"
readonly offset: number
}
Since v3.6.0
Named (interface)
Signature
export interface Named extends Proto {
readonly _tag: "Named"
readonly id: string
/** @internal */
readonly format: Intl.DateTimeFormat
}
Since v3.6.0
Utc (interface)
Signature
export interface Utc extends DateTime.Proto {
readonly _tag: "Utc"
readonly epochMillis: number
partsUtc: DateTime.PartsWithWeekday | undefined
}
Since v3.6.0
Zoned (interface)
Signature
export interface Zoned extends DateTime.Proto {
readonly _tag: "Zoned"
readonly epochMillis: number
readonly zone: TimeZone
adjustedEpochMillis: number | undefined
partsAdjusted: DateTime.PartsWithWeekday | undefined
partsUtc: DateTime.PartsWithWeekday | undefined
}
Since v3.6.0
parts
getPart
Get a part of a DateTime
as a number.
The part will be time zone adjusted.
Example
import * as assert from "node:assert"
import { DateTime } from "effect"
const now = DateTime.unsafeMakeZoned({ year: 2024 }, { timeZone: "Europe/London" })
const year = DateTime.getPart(now, "year")
assert.strictEqual(year, 2024)
Signature
declare const getPart: {
(part: keyof DateTime.PartsWithWeekday): (self: DateTime) => number
(self: DateTime, part: keyof DateTime.PartsWithWeekday): number
}
Since v3.6.0
getPartUtc
Get a part of a DateTime
as a number.
The part will be in the UTC time zone.
Example
import * as assert from "node:assert"
import { DateTime } from "effect"
const now = DateTime.unsafeMake({ year: 2024 })
const year = DateTime.getPartUtc(now, "year")
assert.strictEqual(year, 2024)
Signature
declare const getPartUtc: {
(part: keyof DateTime.PartsWithWeekday): (self: DateTime) => number
(self: DateTime, part: keyof DateTime.PartsWithWeekday): number
}
Since v3.6.0
setParts
Set the different parts of a DateTime
as an object.
The Date will be time zone adjusted.
Signature
declare const setParts: {
(parts: Partial<DateTime.PartsWithWeekday>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsWithWeekday>): A
}
Since v3.6.0
setPartsUtc
Set the different parts of a DateTime
as an object.
Signature
declare const setPartsUtc: {
(parts: Partial<DateTime.PartsWithWeekday>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsWithWeekday>): A
}
Since v3.6.0
toParts
Get the different parts of a DateTime
as an object.
The parts will be time zone adjusted.
Signature
declare const toParts: (self: DateTime) => DateTime.PartsWithWeekday
Since v3.6.0
toPartsUtc
Get the different parts of a DateTime
as an object.
The parts will be in UTC.
Signature
declare const toPartsUtc: (self: DateTime) => DateTime.PartsWithWeekday
Since v3.6.0
time zones
setZone
Set the time zone of a DateTime
, returning a new DateTime.Zoned
.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
const zone = DateTime.zoneUnsafeMakeNamed("Europe/London")
// set the time zone
const zoned: DateTime.Zoned = DateTime.setZone(now, zone)
})
Signature
declare const setZone: {
(zone: TimeZone, options?: { readonly adjustForTimeZone?: boolean | undefined }): (self: DateTime) => Zoned
(self: DateTime, zone: TimeZone, options?: { readonly adjustForTimeZone?: boolean | undefined }): Zoned
}
Since v3.6.0
setZoneNamed
Set the time zone of a DateTime
from an IANA time zone identifier. If the time zone is invalid, None
will be returned.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
// set the time zone, returns an Option
DateTime.setZoneNamed(now, "Europe/London")
})
Signature
declare const setZoneNamed: {
(
zoneId: string,
options?: { readonly adjustForTimeZone?: boolean | undefined }
): (self: DateTime) => Option.Option<Zoned>
(self: DateTime, zoneId: string, options?: { readonly adjustForTimeZone?: boolean | undefined }): Option.Option<Zoned>
}
Since v3.6.0
setZoneOffset
Add a fixed offset time zone to a DateTime
.
The offset is in milliseconds.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
// set the offset time zone in milliseconds
const zoned: DateTime.Zoned = DateTime.setZoneOffset(now, 3 * 60 * 60 * 1000)
})
Signature
declare const setZoneOffset: {
(offset: number, options?: { readonly adjustForTimeZone?: boolean | undefined }): (self: DateTime) => Zoned
(self: DateTime, offset: number, options?: { readonly adjustForTimeZone?: boolean | undefined }): Zoned
}
Since v3.6.0
toUtc
For a DateTime
returns a new DateTime.Utc
.
Example
import { DateTime } from "effect"
const now = DateTime.unsafeMakeZoned({ year: 2024 }, { timeZone: "Europe/London" })
// set as UTC
const utc: DateTime.Utc = DateTime.toUtc(now)
Signature
declare const toUtc: (self: DateTime) => Utc
Since v3.13.0
unsafeSetZoneNamed
Set the time zone of a DateTime
from an IANA time zone identifier. If the time zone is invalid, an IllegalArgumentException
will be thrown.
Example
import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
// set the time zone
DateTime.unsafeSetZoneNamed(now, "Europe/London")
})
Signature
declare const unsafeSetZoneNamed: {
(zoneId: string, options?: { readonly adjustForTimeZone?: boolean | undefined }): (self: DateTime) => Zoned
(self: DateTime, zoneId: string, options?: { readonly adjustForTimeZone?: boolean | undefined }): Zoned
}
Since v3.6.0
zoneFromString
Try parse a TimeZone from a string
Signature
declare const zoneFromString: (zone: string) => Option.Option<TimeZone>
Since v3.6.0
zoneMakeLocal
Create a named time zone from the system’s local time zone.
Signature
declare const zoneMakeLocal: () => TimeZone.Named
Since v3.6.0
zoneMakeNamed
Create a named time zone from a IANA time zone identifier. If the time zone is invalid, None
will be returned.
Signature
declare const zoneMakeNamed: (zoneId: string) => Option.Option<TimeZone.Named>
Since v3.6.0
zoneMakeNamedEffect
Create a named time zone from a IANA time zone identifier. If the time zone is invalid, it will fail with an IllegalArgumentException
.
Signature
declare const zoneMakeNamedEffect: (zoneId: string) => Effect.Effect<TimeZone.Named, IllegalArgumentException>
Since v3.6.0
zoneMakeOffset
Create a fixed offset time zone.
Signature
declare const zoneMakeOffset: (offset: number) => TimeZone.Offset
Since v3.6.0
zoneToString
Format a TimeZone
as a string.
Example
import { DateTime, Effect } from "effect"
// Outputs "+03:00"
DateTime.zoneToString(DateTime.zoneMakeOffset(3 * 60 * 60 * 1000))
// Outputs "Europe/London"
DateTime.zoneToString(DateTime.zoneUnsafeMakeNamed("Europe/London"))
Signature
declare const zoneToString: (self: TimeZone) => string
Since v3.6.0
zoneUnsafeMakeNamed
Attempt to create a named time zone from a IANA time zone identifier.
If the time zone is invalid, an IllegalArgumentException
will be thrown.
Signature
declare const zoneUnsafeMakeNamed: (zoneId: string) => TimeZone.Named
Since v3.6.0
type ids
TimeZoneTypeId
Signature
declare const TimeZoneTypeId: unique symbol
Since v3.6.0
TimeZoneTypeId (type alias)
Signature
type TimeZoneTypeId = typeof TimeZoneTypeId
Since v3.6.0
TypeId
Signature
declare const TypeId: unique symbol
Since v3.6.0
TypeId (type alias)
Signature
type TypeId = typeof TypeId
Since v3.6.0
utils
clamp
Signature
declare const clamp: {
<Min extends DateTime, Max extends DateTime>(options: {
readonly minimum: Min
readonly maximum: Max
}): <A extends DateTime>(self: A) => A | Min | Max
<A extends DateTime, Min extends DateTime, Max extends DateTime>(
self: A,
options: { readonly minimum: Min; readonly maximum: Max }
): A | Min | Max
}
Since v3.6.0