Skip to content

DateTime

在 JavaScript 中处理日期和时间可能具有挑战性。内置的 Date 对象会改变其内部状态,时区处理可能令人困惑。这些设计选择可能在依赖日期时间准确性的应用程序中导致错误,例如调度系统、时间戳服务或日志记录实用程序。

DateTime 模块旨在通过提供以下功能来解决这些限制:

  • 不可变数据:每个 DateTime 都是不可变结构,减少与就地变更相关的错误。
  • 时区支持DateTime 提供强大的时区支持,包括自动夏令时调整。
  • 算术运算:您可以对 DateTime 实例执行算术运算,例如添加或减去持续时间。

DateTime 表示时间中的一个时刻。它可以存储为简单的 UTC 值或与关联时区的值。以这种方式存储时间有助于您管理精确的时间戳和该时间应如何显示或解释的上下文。

DateTime 有两个主要变体:

  1. Utc:一个不可变结构,使用 epochMillis(自 Unix 纪元以来的毫秒数)来表示协调世界时 (UTC) 中的时间点。

  2. Zoned:包括 epochMillis 以及 TimeZone,允许您将偏移量或命名区域(如 “America/New_York”)附加到时间戳。

  • Utc 如果您只需要通用引用而不依赖本地时区,则很简单。
  • Zoned 当您需要跟踪时区信息以执行诸如转换为本地时间或调整夏令时等任务时很有用。

TimeZone 可以是:

  • Offset:表示与 UTC 的固定偏移量(例如,UTC+2 或 UTC-5)。
  • Named:使用命名区域(例如,“Europe/London” 或 “America/New_York”),自动考虑特定区域的规则,如夏令时变化。

以下是 DateTime 类型的 TypeScript 定义:

type DateTime = Utc | Zoned
interface Utc {
readonly _tag: "Utc"
readonly epochMillis: number
}
interface Zoned {
readonly _tag: "Zoned"
readonly epochMillis: number
readonly zone: TimeZone
}
type TimeZone = TimeZone.Offset | TimeZone.Named
declare namespace TimeZone {
interface Offset {
readonly _tag: "Offset"
readonly offset: number
}
interface Named {
readonly _tag: "Named"
readonly id: string
}
}

DateTime.Parts 类型定义了日期的主要组件,例如年、月、日、小时、分钟和秒。

namespace DateTime {
interface Parts {
readonly millis: number
readonly seconds: number
readonly minutes: number
readonly hours: number
readonly day: number
readonly month: number
readonly year: number
}
interface PartsWithWeekday extends Parts {
readonly weekDay: number
}
}

DateTime.Input 类型是一个灵活的输入类型,可用于创建 DateTime 实例。它可以是以下之一:

  • DateTime 实例
  • JavaScript Date 对象
  • 表示自 Unix 纪元以来毫秒数的数值
  • 具有部分日期组件的对象(例如,{ year: 2024, month: 1, day: 1 }
  • 可以由 JavaScript 的 Date.parse 解析的字符串
namespace DateTime {
type Input = DateTime | Partial<Parts> | Date | number | string
}

Utc 是一个不可变结构,使用 epochMillis(自 Unix 纪元以来的毫秒数)来表示协调世界时 (UTC) 中的时间点。

从 JavaScript Date 创建 Utc。 如果提供的 Date 无效,则抛出 IllegalArgumentException

当传递 Date 对象时,它会转换为 Utc 实例。时间被解释为执行代码的系统的本地时间,然后调整为 UTC。这确保了日期和时间的一致、与时区无关的表示。

示例(在意大利将本地时间转换为 UTC)

以下示例假设代码在意大利(CET 时区)的系统上执行:

import {
import DateTime
DateTime
} from "effect"
// 从本地 JavaScript Date 创建 Utc 实例
//
// ┌─── Utc
// ▼
const
const utc: DateTime.Utc
utc
=
import DateTime
DateTime
.
const unsafeFromDate: (date: Date) => DateTime.Utc

Create a DateTime from a Date.

If the Date is invalid, an IllegalArgumentException will be thrown.

@since3.6.0

unsafeFromDate
(new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2025-01-01 04:00:00"))
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const utc: DateTime.Utc
utc
)
// 输出: DateTime.Utc(2025-01-01T03:00:00.000Z)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const utc: DateTime.Utc
utc
.
Utc.epochMillis: number
epochMillis
)
// 输出: 1735700400000

解释

  • 本地时间 2025-01-01 04:00:00(在意大利,CET)通过减去时区偏移量(1月份为 UTC+1)转换为 UTC
  • 结果,UTC 时间变为 2025-01-01 03:00:00.000Z
  • epochMillis 提供自 Unix 纪元以来的毫秒数形式的相同时间,确保 UTC 时间戳的精确数值表示。

DateTime.Input 创建 Utc

示例(使用 unsafeMake 创建 DateTime)

以下示例假设代码在意大利(CET 时区)的系统上执行:

import {
import DateTime
DateTime
} from "effect"
// 从 JavaScript Date
const
const utc1: DateTime.Utc
utc1
=
import DateTime
DateTime
.
const unsafeMake: <Date>(input: Date) => DateTime.Utc

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.unsafeMake(new Date())
// from parts
DateTime.unsafeMake({ year: 2024 })
// from string
DateTime.unsafeMake("2024-01-01")

unsafeMake
(new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2025-01-01 04:00:00"))
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const utc1: DateTime.Utc
utc1
)
// 输出: DateTime.Utc(2025-01-01T03:00:00.000Z)
// 从部分日期组件
const
const utc2: DateTime.Utc
utc2
=
import DateTime
DateTime
.
const unsafeMake: <{
year: number;
}>(input: {
year: number;
}) => DateTime.Utc

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.unsafeMake(new Date())
// from parts
DateTime.unsafeMake({ year: 2024 })
// from string
DateTime.unsafeMake("2024-01-01")

unsafeMake
({
year: number
year
: 2025 })
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const utc2: DateTime.Utc
utc2
)
// 输出: DateTime.Utc(2025-01-01T00:00:00.000Z)
// 从字符串
const
const utc3: DateTime.Utc
utc3
=
import DateTime
DateTime
.
const unsafeMake: <"2025-01-01">(input: "2025-01-01") => DateTime.Utc

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.unsafeMake(new Date())
// from parts
DateTime.unsafeMake({ year: 2024 })
// from string
DateTime.unsafeMake("2024-01-01")

unsafeMake
("2025-01-01")
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const utc3: DateTime.Utc
utc3
)
// 输出: DateTime.Utc(2025-01-01T00:00:00.000Z)

解释

  • 本地时间 2025-01-01 04:00:00(在意大利,CET)通过减去时区偏移量(1月份为 UTC+1)转换为 UTC
  • 结果,UTC 时间变为 2025-01-01 03:00:00.000Z

类似于 unsafeMake,但如果输入无效,则返回 Option 而不是抛出错误。 如果输入无效,则返回 None。如果有效,则返回包含 UtcSome

示例(安全地创建 DateTime)

以下示例假设代码在意大利(CET 时区)的系统上执行:

import {
import DateTime
DateTime
} from "effect"
// 从 JavaScript Date
const
const maybeUtc1: Option<DateTime.Utc>
maybeUtc1
=
import DateTime
DateTime
.
const make: <Date>(input: Date) => Option<DateTime.Utc>

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

If the input is invalid, None will be returned.

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.make(new Date())
// from parts
DateTime.make({ year: 2024 })
// from string
DateTime.make("2024-01-01")

make
(new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2025-01-01 04:00:00"))
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const maybeUtc1: Option<DateTime.Utc>
maybeUtc1
)
/*
输出:
{ _id: 'Option', _tag: 'Some', value: '2025-01-01T03:00:00.000Z' }
*/
// 从部分日期组件
const
const maybeUtc2: Option<DateTime.Utc>
maybeUtc2
=
import DateTime
DateTime
.
const make: <{
year: number;
}>(input: {
year: number;
}) => Option<DateTime.Utc>

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

If the input is invalid, None will be returned.

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.make(new Date())
// from parts
DateTime.make({ year: 2024 })
// from string
DateTime.make("2024-01-01")

make
({
year: number
year
: 2025 })
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const maybeUtc2: Option<DateTime.Utc>
maybeUtc2
)
/*
输出:
{ _id: 'Option', _tag: 'Some', value: '2025-01-01T00:00:00.000Z' }
*/
// 从字符串
const
const maybeUtc3: Option<DateTime.Utc>
maybeUtc3
=
import DateTime
DateTime
.
const make: <"2025-01-01">(input: "2025-01-01") => Option<DateTime.Utc>

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

If the input is invalid, None will be returned.

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.make(new Date())
// from parts
DateTime.make({ year: 2024 })
// from string
DateTime.make("2024-01-01")

make
("2025-01-01")
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const maybeUtc3: Option<DateTime.Utc>
maybeUtc3
)
/*
输出:
{ _id: 'Option', _tag: 'Some', value: '2025-01-01T00:00:00.000Z' }
*/

解释

  • 本地时间 2025-01-01 04:00:00(在意大利,CET)通过减去时区偏移量(1月份为 UTC+1)转换为 UTC
  • 结果,UTC 时间变为 2025-01-01 03:00:00.000Z

Zoned 包括 epochMillis 以及 TimeZone,允许您将偏移量或命名区域(如 “America/New_York”)附加到时间戳。

通过将 DateTime.Input 与可选的 TimeZone 组合来创建 Zoned。 这允许您表示具有关联时区的特定时间点。

时区可以通过几种方式提供:

  • 作为 TimeZone 对象
  • 字符串标识符(例如,"Europe/London"
  • 以毫秒为单位的数字偏移量

如果输入或时区无效,则抛出 IllegalArgumentException

示例(创建不指定时区的 Zoned DateTime)

以下示例假设代码在意大利(CET 时区)的系统上执行:

import {
import DateTime
DateTime
} from "effect"
// 基于系统本地时区创建 Zoned DateTime
const
const zoned: DateTime.Zoned
zoned
=
import DateTime
DateTime
.
const unsafeMakeZoned: (input: DateTime.DateTime.Input, options?: {
readonly timeZone?: number | string | DateTime.TimeZone | undefined;
readonly adjustForTimeZone?: boolean | undefined;
readonly disambiguation?: DateTime.Disambiguation | undefined;
}) => DateTime.Zoned

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.

When adjustForTimeZone is true and ambiguous times occur during DST transitions, the disambiguation option controls how to resolve the ambiguity:

  • compatible (default): Choose earlier time for repeated times, later for gaps
  • earlier: Always choose the earlier of two possible times
  • later: Always choose the later of two possible times
  • reject: Throw an error when ambiguous times are encountered

@since3.6.0

@example

import { DateTime } from "effect"
DateTime.unsafeMakeZoned(new Date(), { timeZone: "Europe/London" })

unsafeMakeZoned
(new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2025-01-01 04:00:00"))
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zoned: DateTime.Zoned
zoned
)
// 输出: DateTime.Zoned(2025-01-01T04:00:00.000+01:00)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zoned: DateTime.Zoned
zoned
.
Zoned.zone: DateTime.TimeZone
zone
)
// 输出: TimeZone.Offset(+01:00)

在这里,系统的时区(CET,1月份为 UTC+1)用于创建 Zoned 实例。

示例(指定命名时区)

以下示例假设代码在意大利(CET 时区)的系统上执行:

import {
import DateTime
DateTime
} from "effect"
// 使用指定的命名时区创建 Zoned DateTime
const
const zoned: DateTime.Zoned
zoned
=
import DateTime
DateTime
.
const unsafeMakeZoned: (input: DateTime.DateTime.Input, options?: {
readonly timeZone?: number | string | DateTime.TimeZone | undefined;
readonly adjustForTimeZone?: boolean | undefined;
readonly disambiguation?: DateTime.Disambiguation | undefined;
}) => DateTime.Zoned

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.

When adjustForTimeZone is true and ambiguous times occur during DST transitions, the disambiguation option controls how to resolve the ambiguity:

  • compatible (default): Choose earlier time for repeated times, later for gaps
  • earlier: Always choose the earlier of two possible times
  • later: Always choose the later of two possible times
  • reject: Throw an error when ambiguous times are encountered

@since3.6.0

@example

import { DateTime } from "effect"
DateTime.unsafeMakeZoned(new Date(), { timeZone: "Europe/London" })

unsafeMakeZoned
(new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2025-01-01 04:00:00"), {
timeZone?: string | number | DateTime.TimeZone | undefined
timeZone
: "Europe/Rome"
})
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zoned: DateTime.Zoned
zoned
)
// 输出: DateTime.Zoned(2025-01-01T04:00:00.000+01:00[Europe/Rome])
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zoned: DateTime.Zoned
zoned
.
Zoned.zone: DateTime.TimeZone
zone
)
// 输出: TimeZone.Named(Europe/Rome)

在这种情况下,明确提供了 "Europe/Rome" 时区,导致 Zoned 实例绑定到此命名时区。

默认情况下,输入日期被视为 UTC 值,然后针对指定时区进行调整。要将输入日期解释为在指定时区中,您可以使用 adjustForTimeZone 选项。

示例(调整时区解释)

以下示例假设代码在意大利(CET 时区)的系统上执行:

import {
import DateTime
DateTime
} from "effect"
// 将输入日期解释为在指定时区中
const
const zoned: DateTime.Zoned
zoned
=
import DateTime
DateTime
.
const unsafeMakeZoned: (input: DateTime.DateTime.Input, options?: {
readonly timeZone?: number | string | DateTime.TimeZone | undefined;
readonly adjustForTimeZone?: boolean | undefined;
readonly disambiguation?: DateTime.Disambiguation | undefined;
}) => DateTime.Zoned

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.

When adjustForTimeZone is true and ambiguous times occur during DST transitions, the disambiguation option controls how to resolve the ambiguity:

  • compatible (default): Choose earlier time for repeated times, later for gaps
  • earlier: Always choose the earlier of two possible times
  • later: Always choose the later of two possible times
  • reject: Throw an error when ambiguous times are encountered

@since3.6.0

@example

import { DateTime } from "effect"
DateTime.unsafeMakeZoned(new Date(), { timeZone: "Europe/London" })

unsafeMakeZoned
(new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2025-01-01 04:00:00"), {
timeZone?: string | number | DateTime.TimeZone | undefined
timeZone
: "Europe/Rome",
adjustForTimeZone?: boolean | undefined
adjustForTimeZone
: true
})
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zoned: DateTime.Zoned
zoned
)
// 输出: DateTime.Zoned(2025-01-01T03:00:00.000+01:00[Europe/Rome])
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zoned: DateTime.Zoned
zoned
.
Zoned.zone: DateTime.TimeZone
zone
)
// 输出: TimeZone.Named(Europe/Rome)

解释

  • 不使用 adjustForTimeZone:输入日期被解释为 UTC,然后调整到指定时区。例如,UTC 中的 2025-01-01 04:00:00 在 CET(UTC+1)中变为 2025-01-01T04:00:00.000+01:00
  • 使用 adjustForTimeZone: true:输入日期被解释为在指定时区中。例如,“Europe/Rome”(CET)中的 2025-01-01 04:00:00 被调整为其对应的 UTC 时间,结果为 2025-01-01T03:00:00.000+01:00

makeZoned 函数的工作方式类似于 unsafeMakeZoned,但提供了更安全的方法。当输入无效时,它不会抛出错误,而是返回 Option<Zoned>。 如果输入无效,则返回 None。如果有效,则返回包含 ZonedSome

示例(安全地创建 Zoned DateTime)

import {
import DateTime
DateTime
,
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// ┌─── Option<Zoned>
// ▼
const
const zoned: Option.Option<DateTime.Zoned>
zoned
=
import DateTime
DateTime
.
const makeZoned: (input: DateTime.DateTime.Input, options?: {
readonly timeZone?: number | string | DateTime.TimeZone | undefined;
readonly adjustForTimeZone?: boolean | undefined;
readonly disambiguation?: DateTime.Disambiguation | undefined;
}) => Option.Option<DateTime.Zoned>

Create a DateTime.Zoned using DateTime.make 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.

When adjustForTimeZone is true and ambiguous times occur during DST transitions, the disambiguation option controls how to resolve the ambiguity:

  • compatible (default): Choose earlier time for repeated times, later for gaps
  • earlier: Always choose the earlier of two possible times
  • later: Always choose the later of two possible times
  • reject: Throw an error when ambiguous times are encountered

If the date time input or time zone is invalid, None will be returned.

@since3.6.0

@example

import { DateTime } from "effect"
DateTime.makeZoned(new Date(), { timeZone: "Europe/London" })

makeZoned
(new
var Date: DateConstructor
new (value: number | string | Date) => Date (+3 overloads)
Date
("2025-01-01 04:00:00"), {
timeZone?: string | number | DateTime.TimeZone | undefined
timeZone
: "Europe/Rome"
})
if (
import Option

@since2.0.0

@since2.0.0

Option
.
const isSome: <DateTime.Zoned>(self: Option.Option<DateTime.Zoned>) => self is Option.Some<DateTime.Zoned>

Checks whether an Option contains a value (Some).

@example

import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false

@seeisNone for the opposite check.

@since2.0.0

isSome
(
const zoned: Option.Option<DateTime.Zoned>
zoned
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
("DateTime 是有效的")
}

通过解析格式为 YYYY-MM-DDTHH:mm:ss.sss+HH:MM[IANA timezone identifier] 的字符串来创建 Zoned

如果输入字符串有效,函数返回包含 ZonedSome。如果输入无效,则返回 None

示例(从字符串解析 Zoned DateTime)

import {
import DateTime
DateTime
,
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// ┌─── Option<Zoned>
// ▼
const
const zoned: Option.Option<DateTime.Zoned>
zoned
=
import DateTime
DateTime
.
const makeZonedFromString: (input: string) => Option.Option<DateTime.Zoned>

Create a DateTime.Zoned from a string.

It uses the format: YYYY-MM-DDTHH:mm:ss.sss+HH:MM[Time/Zone].

@since3.6.0

makeZonedFromString
(
"2025-01-01T03:00:00.000+01:00[Europe/Rome]"
)
if (
import Option

@since2.0.0

@since2.0.0

Option
.
const isSome: <DateTime.Zoned>(self: Option.Option<DateTime.Zoned>) => self is Option.Some<DateTime.Zoned>

Checks whether an Option contains a value (Some).

@example

import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false

@seeisNone for the opposite check.

@since2.0.0

isSome
(
const zoned: Option.Option<DateTime.Zoned>
zoned
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
("DateTime 是有效的")
}

使用 Clock 服务提供当前 UTC 时间作为 Effect<Utc>

示例(获取当前 UTC 时间)

import {
import DateTime
DateTime
,
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
} from "effect"
const
const program: Effect.Effect<void, never, never>
program
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const gen: <YieldWrap<Effect.Effect<DateTime.Utc, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<DateTime.Utc, never, never>>, void, never>) => Effect.Effect<void, never, never> (+1 overload)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

Effect.gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

Example

import { Effect } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, Error> =>
discountRate === 0
? Effect.fail(new Error("Discount rate cannot be zero"))
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function* () {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

gen
(function* () {
// ┌─── Utc
// ▼
const
const currentTime: DateTime.Utc
currentTime
= yield*
import DateTime
DateTime
.
const now: Effect.Effect<DateTime.Utc, never, never>

Get the current time using the Clock service and convert it to a DateTime.

@since3.6.0

@example

import { DateTime, Effect } from "effect"
Effect.gen(function* () {
const now = yield* DateTime.now
})

now
})

使用 Date.now() 立即获取当前 UTC 时间,不使用 Clock 服务。

示例(立即获取当前 UTC 时间)

import {
import DateTime
DateTime
} from "effect"
// ┌─── Utc
// ▼
const
const currentTime: DateTime.Utc
currentTime
=
import DateTime
DateTime
.
const unsafeNow: LazyArg
() => DateTime.Utc

Get the current time using Date.now.

@since3.6.0

unsafeNow
()
函数描述
isDateTime检查值是否为 DateTime
isTimeZone检查值是否为 TimeZone
isTimeZoneOffset检查值是否为 TimeZone.Offset
isTimeZoneNamed检查值是否为 TimeZone.Named
isUtc检查 DateTime 是否为 Utc 变体。
isZoned检查 DateTime 是否为 Zoned 变体。

示例(验证 DateTime)

import {
import DateTime
DateTime
} from "effect"
function
function printDateTimeInfo(x: unknown): void
printDateTimeInfo
(
x: unknown
x
: unknown) {
if (
import DateTime
DateTime
.
const isDateTime: (u: unknown) => u is DateTime.DateTime

@since3.6.0

isDateTime
(
x: unknown
x
)) {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
("This is a valid DateTime")
} else {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
("Not a DateTime")
}
}
FunctionDescription
setZoneCreates a Zoned from DateTime by applying the given TimeZone.
setZoneOffsetCreates a Zoned from DateTime using a fixed offset (in ms).
setZoneNamedCreates a Zoned from DateTime from an IANA time zone identifier or returns None if invalid.
unsafeSetZoneNamedCreates a Zoned from DateTime from an IANA time zone identifier or throws if invalid.
zoneUnsafeMakeNamedCreates a TimeZone.Named from a IANA time zone identifier or throws if the identifier is invalid.
zoneMakeNamedCreates a TimeZone.Named from a IANA time zone identifier or returns None if invalid.
zoneMakeNamedEffectCreates a Effect<TimeZone.Named, IllegalArgumentException> from a IANA time zone identifier failing with IllegalArgumentException if invalid
zoneMakeOffsetCreates a TimeZone.Offset from a numeric offset in milliseconds.
zoneMakeLocalCreates a TimeZone.Named from the system’s local time zone.
zoneFromStringAttempts to parse a time zone from a string, returning None if invalid.
zoneToStringReturns a string representation of a TimeZone.

Example (Applying a Time Zone to a DateTime)

import {
import DateTime
DateTime
} from "effect"
// Create a UTC DateTime
//
// ┌─── Utc
// ▼
const
const utc: DateTime.Utc
utc
=
import DateTime
DateTime
.
const unsafeMake: <"2024-01-01">(input: "2024-01-01") => DateTime.Utc

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.unsafeMake(new Date())
// from parts
DateTime.unsafeMake({ year: 2024 })
// from string
DateTime.unsafeMake("2024-01-01")

unsafeMake
("2024-01-01")
// Create a named time zone for New York
//
// ┌─── TimeZone.Named
// ▼
const
const zoneNY: DateTime.TimeZone.Named
zoneNY
=
import DateTime
DateTime
.
const zoneUnsafeMakeNamed: (zoneId: string) => DateTime.TimeZone.Named

Attempt to create a named time zone from a IANA time zone identifier.

If the time zone is invalid, an IllegalArgumentException will be thrown.

@since3.6.0

zoneUnsafeMakeNamed
("America/New_York")
// Apply it to the DateTime
//
// ┌─── Zoned
// ▼
const
const zoned: DateTime.Zoned
zoned
=
import DateTime
DateTime
.
const setZone: (self: DateTime.DateTime, zone: DateTime.TimeZone, options?: {
readonly adjustForTimeZone?: boolean | undefined;
readonly disambiguation?: DateTime.Disambiguation | undefined;
}) => DateTime.Zoned (+1 overload)

Set the time zone of a DateTime, returning a new DateTime.Zoned.

@since3.6.0

@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)
})

setZone
(
const utc: DateTime.Utc
utc
,
const zoneNY: DateTime.TimeZone.Named
zoneNY
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zoned: DateTime.Zoned
zoned
)
// Output: DateTime.Zoned(2023-12-31T19:00:00.000-05:00[America/New_York])

Parses a string to create a DateTime.TimeZone.

This function attempts to interpret the input string as either:

  • A numeric time zone offset (e.g., “GMT”, “+01:00”)
  • An IANA time zone identifier (e.g., “Europe/London”)

If the string matches an offset format, it is converted into a TimeZone.Offset. Otherwise, it attempts to create a TimeZone.Named using the input.

If the input string is invalid, Option.none() is returned.

Example (Parsing a Time Zone from a String)

import {
import DateTime
DateTime
,
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// Attempt to parse a numeric offset
const
const offsetZone: Option.Option<DateTime.TimeZone>
offsetZone
=
import DateTime
DateTime
.
const zoneFromString: (zone: string) => Option.Option<DateTime.TimeZone>

Try parse a TimeZone from a string

@since3.6.0

zoneFromString
("+01:00")
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import Option

@since2.0.0

@since2.0.0

Option
.
const isSome: <DateTime.TimeZone>(self: Option.Option<DateTime.TimeZone>) => self is Option.Some<DateTime.TimeZone>

Checks whether an Option contains a value (Some).

@example

import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false

@seeisNone for the opposite check.

@since2.0.0

isSome
(
const offsetZone: Option.Option<DateTime.TimeZone>
offsetZone
))
// Output: true
// Attempt to parse an IANA time zone
const
const namedZone: Option.Option<DateTime.TimeZone>
namedZone
=
import DateTime
DateTime
.
const zoneFromString: (zone: string) => Option.Option<DateTime.TimeZone>

Try parse a TimeZone from a string

@since3.6.0

zoneFromString
("Europe/London")
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import Option

@since2.0.0

@since2.0.0

Option
.
const isSome: <DateTime.TimeZone>(self: Option.Option<DateTime.TimeZone>) => self is Option.Some<DateTime.TimeZone>

Checks whether an Option contains a value (Some).

@example

import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false

@seeisNone for the opposite check.

@since2.0.0

isSome
(
const namedZone: Option.Option<DateTime.TimeZone>
namedZone
))
// Output: true
// Invalid input
const
const invalidZone: Option.Option<DateTime.TimeZone>
invalidZone
=
import DateTime
DateTime
.
const zoneFromString: (zone: string) => Option.Option<DateTime.TimeZone>

Try parse a TimeZone from a string

@since3.6.0

zoneFromString
("Invalid/Zone")
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import Option

@since2.0.0

@since2.0.0

Option
.
const isSome: <DateTime.TimeZone>(self: Option.Option<DateTime.TimeZone>) => self is Option.Some<DateTime.TimeZone>

Checks whether an Option contains a value (Some).

@example

import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false

@seeisNone for the opposite check.

@since2.0.0

isSome
(
const invalidZone: Option.Option<DateTime.TimeZone>
invalidZone
))
// Output: false
FunctionDescription
distanceReturns the difference (in ms) between two DateTimes.
distanceDurationEitherReturns a Left or Right Duration depending on order.
distanceDurationReturns a Duration indicating how far apart two times are.
minReturns the earlier of two DateTime values.
maxReturns the later of two DateTime values.
greaterThan, greaterThanOrEqualTo, etc.Checks ordering between two DateTime values.
betweenChecks if a DateTime lies within the given bounds.
isFuture, isPast, unsafeIsFuture, etc.Checks if a DateTime is in the future or past.

Example (Finding the Distance Between Two DateTimes)

import {
import DateTime
DateTime
} from "effect"
const
const utc1: DateTime.Utc
utc1
=
import DateTime
DateTime
.
const unsafeMake: <"2025-01-01T00:00:00Z">(input: "2025-01-01T00:00:00Z") => DateTime.Utc

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.unsafeMake(new Date())
// from parts
DateTime.unsafeMake({ year: 2024 })
// from string
DateTime.unsafeMake("2024-01-01")

unsafeMake
("2025-01-01T00:00:00Z")
const
const utc2: DateTime.Utc
utc2
=
import DateTime
DateTime
.
const add: <DateTime.Utc>(self: DateTime.Utc, parts: Partial<DateTime.DateTime.PartsForMath>) => DateTime.Utc (+1 overload)

Add the given amount of unit's to a DateTime.

The time zone is taken into account when adding days, weeks, months, and years.

@since3.6.0

@example

import { DateTime } from "effect"
// add 5 minutes
DateTime.unsafeMake(0).pipe(
DateTime.add({ minutes: 5 })
)

add
(
const utc1: DateTime.Utc
utc1
, {
days?: number
days
: 1 })
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import DateTime
DateTime
.
const distance: (self: DateTime.DateTime, other: DateTime.DateTime) => number (+1 overload)

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.

@since3.6.0

@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)
})

distance
(
const utc1: DateTime.Utc
utc1
,
const utc2: DateTime.Utc
utc2
))
// Output: 86400000 (one day)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import DateTime
DateTime
.
const distanceDurationEither: (self: DateTime.DateTime, other: DateTime.DateTime) => Either<Duration, Duration> (+1 overload)

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.

@since3.6.0

@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)
})

distanceDurationEither
(
const utc1: DateTime.Utc
utc1
,
const utc2: DateTime.Utc
utc2
))
/*
Output:
{
_id: 'Either',
_tag: 'Right',
right: { _id: 'Duration', _tag: 'Millis', millis: 86400000 }
}
*/
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import DateTime
DateTime
.
const distanceDuration: (self: DateTime.DateTime, other: DateTime.DateTime) => Duration (+1 overload)

Calulate the distance between two DateTime values.

@since3.6.0

@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)
})

distanceDuration
(
const utc1: DateTime.Utc
utc1
,
const utc2: DateTime.Utc
utc2
))
// Output: { _id: 'Duration', _tag: 'Millis', millis: 86400000 }
FunctionDescription
toDateUtcReturns a JavaScript Date in UTC.
toDateApplies the time zone (if present) and converts to a JavaScript Date.
zonedOffsetFor a Zoned DateTime, returns the time zone offset in ms.
zonedOffsetIsoFor a Zoned DateTime, returns an ISO offset string like “+01:00”.
toEpochMillisReturns the Unix epoch time in milliseconds.
removeTimeReturns a Utc with the time cleared (only date remains).
FunctionDescription
toPartsReturns time zone adjusted date parts (including weekday).
toPartsUtcReturns UTC date parts (including weekday).
getPart / getPartUtcRetrieves a specific part (e.g., "year" or "month") from the date.
setParts / setPartsUtcUpdates certain parts of a date, preserving or ignoring the time zone.

Example (Extracting Parts from a DateTime)

import {
import DateTime
DateTime
} from "effect"
const
const zoned: DateTime.Zoned
zoned
=
import DateTime
DateTime
.
const setZone: (self: DateTime.DateTime, zone: DateTime.TimeZone, options?: {
readonly adjustForTimeZone?: boolean | undefined;
readonly disambiguation?: DateTime.Disambiguation | undefined;
}) => DateTime.Zoned (+1 overload)

Set the time zone of a DateTime, returning a new DateTime.Zoned.

@since3.6.0

@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)
})

setZone
(
import DateTime
DateTime
.
const unsafeMake: <"2024-01-01">(input: "2024-01-01") => DateTime.Utc

Create a DateTime from one of the following:

  • A DateTime
  • A Date instance (invalid dates will throw an IllegalArgumentException)
  • The number of milliseconds since the Unix epoch
  • An object with the parts of a date
  • A string that can be parsed by Date.parse

@since3.6.0

@example

import { DateTime } from "effect"
// from Date
DateTime.unsafeMake(new Date())
// from parts
DateTime.unsafeMake({ year: 2024 })
// from string
DateTime.unsafeMake("2024-01-01")

unsafeMake
("2024-01-01"),
import DateTime
DateTime
.
const zoneUnsafeMakeNamed: (zoneId: string) => DateTime.TimeZone.Named

Attempt to create a named time zone from a IANA time zone identifier.

If the time zone is invalid, an IllegalArgumentException will be thrown.

@since3.6.0

zoneUnsafeMakeNamed
("Europe/Rome")
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import DateTime
DateTime
.
const getPart: (self: DateTime.DateTime, part: keyof DateTime.DateTime.PartsWithWeekday) => number (+1 overload)

Get a part of a DateTime as a number.

The part will be time zone adjusted.

@since3.6.0

@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)

getPart
(
const zoned: DateTime.Zoned
zoned
, "month"))
// Output: 1
FunctionDescription
addDurationAdds the given Duration to a DateTime.
subtractDurationSubtracts the given Duration from a DateTime.
addAdds numeric parts (e.g., { hours: 2 }) to a DateTime.
subtractSubtracts numeric parts.
startOfMoves a DateTime to the start of the given unit (e.g., the beginning of a day or month).
endOfMoves a DateTime to the end of the given unit.
nearestRounds a DateTime to the nearest specified unit.
FunctionDescription
formatFormats a DateTime as a string using the DateTimeFormat API.
formatLocalUses the system’s local time zone and locale for formatting.
formatUtcForces UTC formatting.
formatIntlUses a provided Intl.DateTimeFormat.
formatIsoReturns an ISO 8601 string in UTC.
formatIsoDateReturns an ISO date string, adjusted for the time zone.
formatIsoDateUtcReturns an ISO date string in UTC.
formatIsoOffsetFormats a Zoned as a string with an offset like “+01:00”.
formatIsoZonedFormats a Zoned in the form YYYY-MM-DDTHH:mm:ss.sss+HH:MM[Zone].
FunctionDescription
CurrentTimeZoneA service tag for the current time zone.
setZoneCurrentSets a DateTime to use the current time zone.
withCurrentZoneProvides an effect with a specified time zone.
withCurrentZoneLocalUses the system’s local time zone for the effect.
withCurrentZoneOffsetUses a fixed offset (in ms) for the effect.
withCurrentZoneNamedUses a named time zone identifier (e.g., “Europe/London”).
nowInCurrentZoneRetrieves the current time as a Zoned in the configured time zone.
layerCurrentZoneCreates a Layer providing the CurrentTimeZone service.
layerCurrentZoneOffsetCreates a Layer from a fixed offset.
layerCurrentZoneNamedCreates a Layer from a named time zone, failing if invalid.
layerCurrentZoneLocalCreates a Layer from the system’s local time zone.

Example (Using the Current Time Zone in an Effect)

import {
import DateTime
DateTime
,
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
} from "effect"
// Retrieve the current time in the "Europe/London" time zone
const
const program: Effect.Effect<void, IllegalArgumentException, never>
program
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const gen: <YieldWrap<Effect.Effect<DateTime.Zoned, never, DateTime.CurrentTimeZone>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<DateTime.Zoned, never, DateTime.CurrentTimeZone>>, void, never>) => Effect.Effect<void, never, DateTime.CurrentTimeZone> (+1 overload)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

Effect.gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

Example

import { Effect } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, Error> =>
discountRate === 0
? Effect.fail(new Error("Discount rate cannot be zero"))
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function* () {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

gen
(function* () {
const
const zonedNow: DateTime.Zoned
zonedNow
= yield*
import DateTime
DateTime
.
const nowInCurrentZone: Effect.Effect<DateTime.Zoned, never, DateTime.CurrentTimeZone>

Get the current time as a DateTime.Zoned, using the CurrentTimeZone.

@since3.6.0

@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"))

nowInCurrentZone
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const zonedNow: DateTime.Zoned
zonedNow
)
}).
Pipeable.pipe<Effect.Effect<void, never, DateTime.CurrentTimeZone>, Effect.Effect<void, IllegalArgumentException, never>>(this: Effect.Effect<void, never, DateTime.CurrentTimeZone>, ab: (_: Effect.Effect<void, never, DateTime.CurrentTimeZone>) => Effect.Effect<void, IllegalArgumentException, never>): Effect.Effect<void, IllegalArgumentException, never> (+21 overloads)
pipe
(
import DateTime
DateTime
.
const withCurrentZoneNamed: (zone: string) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | IllegalArgumentException, Exclude<R, DateTime.CurrentTimeZone>> (+1 overload)

Provide the CurrentTimeZone to an effect using an IANA time zone identifier.

If the time zone is invalid, it will fail with an IllegalArgumentException.

@since3.6.0

@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"))

withCurrentZoneNamed
("Europe/London"))
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const runFork: <void, IllegalArgumentException>(effect: Effect.Effect<void, IllegalArgumentException, never>, options?: RunForkOptions) => RuntimeFiber<void, IllegalArgumentException>

Runs an effect in the background, returning a fiber that can be observed or interrupted.

Unless you specifically need a Promise or synchronous operation, runFork is a good default choice.

Details

This function is the foundational way to execute an effect in the background. It creates a "fiber," a lightweight, cooperative thread of execution that can be observed (to access its result), interrupted, or joined. Fibers are useful for concurrent programming and allow effects to run independently of the main program flow.

Once the effect is running in a fiber, you can monitor its progress, cancel it if necessary, or retrieve its result when it completes. If the effect fails, the fiber will propagate the failure, which you can observe and handle.

When to Use

Use this function when you need to run an effect in the background, especially if the effect is long-running or performs periodic tasks. It's suitable for tasks that need to run independently but might still need observation or management, like logging, monitoring, or scheduled tasks.

This function is ideal if you don't need the result immediately or if the effect is part of a larger concurrent workflow.

Example (Running an Effect in the Background)

import { Effect, Console, Schedule, Fiber } from "effect"
// ┌─── Effect<number, never, never>
// ▼
const program = Effect.repeat(
Console.log("running..."),
Schedule.spaced("200 millis")
)
// ┌─── RuntimeFiber<number, never>
// ▼
const fiber = Effect.runFork(program)
setTimeout(() => {
Effect.runFork(Fiber.interrupt(fiber))
}, 500)

@since2.0.0

runFork
(
const program: Effect.Effect<void, IllegalArgumentException, never>
program
)
/*
Example Output:
DateTime.Zoned(2025-01-06T18:36:38.573+00:00[Europe/London])
*/