t3-code-android-nightly/.repos/effect-smol/ai-docs/src/07_datetime/10_creating-and-formatting.ts
Julius Marminge 108e01746c
Upgrade Effect and Alchemy betas (#4643)
Prepare Relay production infrastructure for the PlanetScale PS_20 HA topology, upgrade Effect and Alchemy to compatible betas, reconcile migration state, and preserve patched MCP session termination behavior.\n\nCo-authored-by: codex <codex@users.noreply.github.com>
2026-07-27 16:21:02 +02:00

30 lines
1.1 KiB
TypeScript

/**
* @title Creating and formatting DateTime values
*
* Parse incoming date values safely, use Clock-powered current time, and format
* instants for API payloads or user-facing labels.
*/
import { DateTime, Effect, Option } from "effect"
Effect.gen(function*() {
// Use DateTime.now to get the current time from Effect's Clock service.
// Using the Clock service ensures tests can use the `TestClock` module to
// control time.
const now = yield* DateTime.now
// Use DateTime.make to parse a date input, such as a user-entered string or a
// epoch timestamp. It returns an Option depending on whether the input was
// valid.
const parsedOption: Option.Option<DateTime.Utc> = DateTime.make("2024-06-15T14:30:00.000Z")
// you can then use the Option apis to unwrap the value
Option.getOrUndefined(parsedOption)
// Calendar/date-time math returns a new DateTime value; the original value is
// immutable.
const endsAt = now.pipe(DateTime.add({ hours: 2 }))
// The DateTime.format* functions can be used to convert a DateTime value to
// differen formats.
yield* Effect.log("ISO string:", DateTime.formatIso(endsAt))
})