Date overview

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

Added in v0.1.0


Table of contents


0 Types

Milliseconds (type alias)

Newtype representing milliseconds.

Signature

export type Milliseconds = Newtype<MillisecondsSymbol, number>
type Milliseconds = Newtype MillisecondsSymbol number

Example

import { Milliseconds, mkMilliseconds } from 'fp-ts-std/Date'

const second: Milliseconds = mkMilliseconds(1000)

Added in v0.7.0

1 Typeclass Instances

fieldMilliseconds

Field instance for Milliseconds, enabling arithmetic over the type.

Signature

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

Example

import { fieldMilliseconds, mkMilliseconds } from 'fp-ts-std/Date'

assert.strictEqual(fieldMilliseconds.add(mkMilliseconds(2), mkMilliseconds(3)), mkMilliseconds(5))

Added in v0.7.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

Example

import { isoMilliseconds, mkMilliseconds } from 'fp-ts-std/Date'
import { add } from 'fp-ts-std/Number'

assert.strictEqual(isoMilliseconds.modify(add(3))(mkMilliseconds(2)), mkMilliseconds(5))

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

Example

import { ordMilliseconds, mkMilliseconds } from 'fp-ts-std/Date'
import { LT } from 'fp-ts-std/Ordering'

assert.strictEqual(ordMilliseconds.compare(mkMilliseconds(0), mkMilliseconds(1)), LT)

Added in v0.7.0

3 Functions

fromMilliseconds

Get a Date from Milliseconds.

Signature

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

Example

import { fromMilliseconds, mkMilliseconds } from 'fp-ts-std/Date'

assert.deepStrictEqual(fromMilliseconds(mkMilliseconds(123)), new Date(123))

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

mkMilliseconds

Lift a number to the Milliseconds newtype.

Signature

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

Example

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

const second = mkMilliseconds(1000)

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

Example

import { now, mkMilliseconds } from 'fp-ts-std/Date'

const x1970 = mkMilliseconds(0)
const x2065 = mkMilliseconds(3000000000000)

assert.strictEqual(now() > x1970, true)
assert.strictEqual(now() < x2065, true)

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

toUTCString

Returns a date converted to a string using Universal Coordinated Time (UTC).

Signature

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

Example

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

const d = new Date()

assert.strictEqual(toUTCString(d), d.toUTCString())

Added in v0.14.0

unMilliseconds

Unwrap a Milliseconds newtype back to its underlying number representation.

Signature

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

Example

import { flow } from 'fp-ts/function'
import { Endomorphism } from 'fp-ts/Endomorphism'
import { mkMilliseconds, unMilliseconds } from 'fp-ts-std/Date'

const redundant: Endomorphism<number> = flow(mkMilliseconds, unMilliseconds)

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