Link Search Menu Expand Document

Date overview

Various functions to aid in working with JavaScript’s Date object.

Added in v0.1.0


Table of contents


utils

Milliseconds (type alias)

Newtype representing milliseconds.

Signature

export type Milliseconds = Newtype<{ readonly Milliseconds: unique symbol }, number>
newtype Milliseconds = number

Added in v0.7.0

fieldMilliseconds

Field instance for Milliseconds, enabling arithmetic over the type.

Signature

export declare const fieldMilliseconds: Field<Milliseconds>
fieldMilliseconds :: Field Milliseconds

Added in v0.7.0

fromMilliseconds

Get a Date from Milliseconds.

Signature

export declare const fromMilliseconds: (x: Milliseconds) => Date
fromMilliseconds :: Milliseconds -> Date

Added in v0.7.0

getTime

Get the time in milliseconds from a Date.

Signature

export declare const getTime: (x: Date) => Milliseconds
getTime :: Date -> Milliseconds

Example

import { getTime } from 'fp-ts-std/Date'

const d = new Date()

assert.strictEqual(getTime(d), d.getTime())

Added in v0.1.0

isDate

Check if a foreign value is a Date.

Signature

export declare const isDate: Refinement<unknown, Date>
isDate :: Refinement unknown Date

Example

import { isDate } from 'fp-ts-std/Date'

assert.strictEqual(isDate(new Date()), true)
assert.strictEqual(isDate({ not: { a: 'date' } }), false)

Added in v0.1.0

isValid

Check if a Date is actually valid. (We all love JavaScript, don’t we?)

Signature

export declare const isValid: Predicate<Date>
isValid :: Predicate Date

Example

import { isValid } from 'fp-ts-std/Date'

const valid = new Date()
const invalid = new Date('this will not parse')

assert.strictEqual(isValid(valid), true)
assert.strictEqual(isValid(invalid), false)

Added in v0.1.0

isoMilliseconds

Iso instance for Milliseconds, enabling the use of lenses over the newtype pertaining to its isomorphic nature.

Signature

export declare const isoMilliseconds: Iso<Milliseconds, number>
isoMilliseconds :: Iso Milliseconds number

Added in v0.7.0

mkMilliseconds

Lift a number to the Milliseconds newtype.

Signature

export declare const mkMilliseconds: (a: number) => Milliseconds
mkMilliseconds :: number -> Milliseconds

Added in v0.7.0

now

Get the time since the Unix Epoch in Milliseconds from a Date.

Signature

export declare const now: IO<Milliseconds>
now :: IO Milliseconds

Added in v0.7.0

ordMilliseconds

Ord instance for Milliseconds, enabling comparison between different instances of the type.

Signature

export declare const ordMilliseconds: Ord<Milliseconds>
ordMilliseconds :: Ord Milliseconds

Added in v0.7.0

parseDate

Safely parse a date.

Signature

export declare const parseDate: (ts: string | number) => Option<Date>
parseDate :: string | number -> Option Date

Example

import { parseDate } from 'fp-ts-std/Date'
import * as O from 'fp-ts/Option'

const valid = 0
const invalid = 'this will not parse'

assert.deepStrictEqual(parseDate(valid), O.some(new Date(valid)))
assert.deepStrictEqual(parseDate(invalid), O.none)

Added in v0.1.0

toISOString

Returns a date as a string value in ISO format.

Signature

export declare const toISOString: (x: Date) => string
toISOString :: Date -> string

Example

import { toISOString } from 'fp-ts-std/Date'

const d = new Date()

assert.strictEqual(toISOString(d), d.toISOString())

Added in v0.1.0

unMilliseconds

Unwrap a Milliseconds newtype back to its underlying number representation.

Signature

export declare const unMilliseconds: (s: Milliseconds) => number
unMilliseconds :: Milliseconds -> number

Added in v0.7.0

unsafeParseDate

Parse a date, leaving open the risk of a failure to parse resulting in an invalid Date being returned.

Signature

export declare const unsafeParseDate: (x: string | number) => Date
unsafeParseDate :: string | number -> Date

Example

import { unsafeParseDate } from 'fp-ts-std/Date'

const valid = 0
const invalid = 'this will not parse'

assert.strictEqual(unsafeParseDate(valid).getTime(), 0)
assert.strictEqual(unsafeParseDate(invalid).getTime(), NaN)

Added in v0.1.0