npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

io-ts-datetime

v1.3.2

Published

io-ts datetime types with luxon

Downloads

180

Readme

io-ts-datetime

NPM Version NPM Downloads

io-ts codec types for dates, datetimes using luxon date time library

Install

npm i io-ts-datetime

Note. luxon, fp-ts, and io-ts are peer dependencies for io-ts-datetime

Usage

dateTimeFromDate(decodeOptions, encodeOptions)

returns a codec that decodes DateTime from a Date instance and encodes back to a string.

decodeOptions

  • zone - (string | Zone) (default 'local') use this zone if no offset is specified in the input string itself. Will also convert the time to this zone

encodeOptions

the same as below

dateTimeFromFormat(decodeOptions, encodeOptions)

returns a codec that decodes DateTime from a string and encodes back to a string.

When decodeOptions or encodeOptions are ommited the ISO DateTime format will be used.

When decodeOptions set but encodeOptions are omitted will encode into the same format as set in decodeOptions

decodeOptions

  • format - "ISO", "SQL", string format. For format tokens look into table of tokens
  • zone - (string | Zone) (default 'local') use this zone if no offset is specified in the input string itself. Will also convert the time to this zone
  • setZone - boolean (default false) override the zone with a fixed-offset zone specified in the string itself, if it specifies one
  • locale - string (default 'system'slocale') a locale to set on the resulting DateTime instance
  • outputCalendar - string the output calendar to set on the resulting DateTime instance
  • numberingSystem - string the numbering system to set on the resulting DateTime instance

encodeOptions

  • format - "ISODate", "ISO", "Basic", "Extended", "SQL", string format. For format tokens look into table of tokens
    • "Extended" is the same as "ISO"
    • "Basic" is ISO format without - and : delimeters

Other encode options depend on the format.

  • ISODate - there is no additional options
  • ISO, Basic, Extended - accepts all toISO method options
    • suppressSeconds - boolean (default false) exclude seconds from the format if they're 0
    • suppressMilliseconds - boolean (default false) exclude milliseconds from the format if they're 0
    • includeOffset - boolean (default true) include the offset, such as 'Z' or '-04:00'
    • extendedZone - boolean (default false) add the time zone format extension
  • SQL - accepts all toSQL method options
    • includeZone - boolean (default false) include the zone, such as 'America/New_York'. Overrides includeOffset.
    • includeOffset - boolean (default true) include the offset, such as 'Z' or '-04:00'
    • includeOffsetSpace - boolean (default true) include the space between the time and the offset, such as '05:15:16.345 -04:00'
  • Custom format - accepts all toFormat method options to override the configuration options on this DateTime

Example

const ISOCodec = dateTimeFromFormat();
const dateTime = ISOCodec.decode("2023-03-14T14:25:22.663-04:00").right;
const isoString = ISOCodec.encode(dateTime);
// "2023-03-14T14:25:22.663-04:00"

const SQLCodec = dateTimeFromFormat({ format: "SQL" }, { format: "ISODate" });
const dateTime = SQLCodec.decode("2023-03-14 14:25:22").right;
const isoDateString = SQLCodec.encode(dateTime);
// "2023-03-14"

From and To Number

There are two extra helper codecs for the cases when date has to be a number.

fromNumber

wraps the codec to validate the input as number and passes it through down to codec as a string for future parsing

toNumber

wraps the codec to represent encode results as a number

const codec = dateTimeFromFormat(
  { format: "yyyyMMdd" },
  { format: "yyyyMMdd" },
);

const dateFromNumber = fromNumber(codec);
const dateToNumber = toNumber(codec);
const dateFromAndToNumber = toNumber(dateFromNumber);

License

MIT