Skip to main content
Home

Utility functions for working with dates

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
94%
Published
a month ago (5.0.0)
function datification
datification(date: string | Date): Result<Date, Error>

Converts a string or a Date object into a Date object.

If the input is a string, it assumes the string is a valid date string (ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ // ie: 2024-03-06T16:09:03Z). If the input is already a Date object, it returns the input as is.

Examples

Example 1

import { assertEquals } from '@std/assert'

// Valid ISO 8601 string
const result = datification('2022-01-01T12:00')
assertEquals(result.error, undefined)
assertEquals(result.data?.toISOString(), new Date('2022-01-01T12:00').toISOString())

Example 2

import { assertEquals } from '@std/assert'

// Date object input
const inputDate = new Date('2022-01-01T12:00')
const result = datification(inputDate)
assertEquals(result.error, undefined)
assertEquals(result.data, inputDate)

Example 3

import { assert } from '@std/assert'

// Invalid format
const result = datification('01012000')
assert(result.error instanceof Error)
assert(result.error.message.includes('Invalid date format'))

Parameters

date: string | Date
  • The input to convert into a Date object. If it's a string, it should be in ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ).

Return Type

A Result containing either the converted Date object or an Error if the format is invalid.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@edouardmisset/date

Import symbol

import { datification } from "@edouardmisset/date/datification.ts";
or

Import directly with a jsr specifier

import { datification } from "jsr:@edouardmisset/date/datification.ts";

Add Package

pnpm i jsr:@edouardmisset/date
or (using pnpm 10.8 or older)
pnpm dlx jsr add @edouardmisset/date

Import symbol

import { datification } from "@edouardmisset/date/datification.ts";

Add Package

yarn add jsr:@edouardmisset/date
or (using Yarn 4.8 or older)
yarn dlx jsr add @edouardmisset/date

Import symbol

import { datification } from "@edouardmisset/date/datification.ts";

Add Package

vlt install jsr:@edouardmisset/date

Import symbol

import { datification } from "@edouardmisset/date/datification.ts";

Add Package

npx jsr add @edouardmisset/date

Import symbol

import { datification } from "@edouardmisset/date/datification.ts";

Add Package

bunx jsr add @edouardmisset/date

Import symbol

import { datification } from "@edouardmisset/date/datification.ts";