Skip to content

Commit 1f47e4e

Browse files
vinassefranchetim-smart
andcommittedMar 20, 2025
Add DateTime.nowAsDate (#4511)
Co-authored-by: Tim <hello@timsmart.co>
1 parent c7fac0c commit 1f47e4e

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed
 

‎.changeset/big-suns-roll.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
Add DateTime.nowAsDate creator

‎packages/effect/src/DateTime.ts

+16
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,22 @@ export const makeZonedFromString: (input: string) => Option.Option<Zoned> = Inte
425425
*/
426426
export const now: Effect.Effect<Utc> = Internal.now
427427

428+
/**
429+
* Get the current time using the `Clock` service.
430+
*
431+
* @since 3.14.0
432+
* @category constructors
433+
* @example
434+
* ```ts
435+
* import { DateTime, Effect } from "effect"
436+
*
437+
* Effect.gen(function* () {
438+
* const now = yield* DateTime.nowAsDate
439+
* })
440+
* ```
441+
*/
442+
export const nowAsDate: Effect.Effect<Date> = Internal.nowAsDate
443+
428444
/**
429445
* Get the current time using `Date.now`.
430446
*

‎packages/effect/src/internal/dateTime.ts

+3
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ export const makeZonedFromString = (input: string): Option.Option<DateTime.Zoned
299299
/** @internal */
300300
export const now: Effect.Effect<DateTime.Utc> = core.map(Clock.currentTimeMillis, makeUtc)
301301

302+
/** @internal */
303+
export const nowAsDate: Effect.Effect<Date> = core.map(Clock.currentTimeMillis, (millis) => new Date(millis))
304+
302305
/** @internal */
303306
export const unsafeNow: LazyArg<DateTime.Utc> = () => makeUtc(Date.now())
304307

‎packages/effect/test/DateTime.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,12 @@ describe("DateTime", () => {
438438
strictEqual(dt.toJSON(), "2023-12-31T12:00:00.000Z")
439439
}))
440440
})
441+
describe("nowAsDate", () => {
442+
it.effect("should return the current Date", () =>
443+
Effect.gen(function*() {
444+
yield* setTo2024NZ
445+
const now = yield* DateTime.nowAsDate
446+
deepStrictEqual(now, new Date("2023-12-31T11:00:00.000Z"))
447+
}))
448+
})
441449
})

0 commit comments

Comments
 (0)
Please sign in to comment.