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

date-str

v1.0.0

Published

A TypeScript type for handling ISO-8601 dates

Downloads

5

Readme

date-str

Safely work with ISO-8601 dates in TypeScript

Working with the concept of "just a date" in JavaScript can be a challenge, as the built-in Date type actually represents a date and a time. When you start making assumptions involving time zones, properly extracting the correct date from the Date object can lead to errors. The best solution I've found is, when I need to work with a date, to store it as an ISO-8601 date string.

⚠️ Note: Although YYYYMMDD is technically a valid date representation according to ISO-8601, this library only works with dates of the format YYYY-MM-DD.

This package adds a branded type representing an ISO-8601 date. It is based on a these excellent articles published on the Atomic Object Blog:

Installation

npm install date-str

Usage

ES Module

import { DateStr, isDateStr, toDateStr } from 'date-str';

CommonJS

const { DateStr, isDateStr, toDateStr } = require('date-str');

Example

interface Person {
  name: string,
  birthday: DateStr
}

const ralph: Person = {
  name: 'Ralph',
  birthday: toDateStr(new Date())
}

ralph.birthday = toDateStr('2012-10-19');

function datePrinter(date: DateStr | Date) {
  if (isDateStr(date)) {
    console.log(date);
  } else {
    console.log(date.toISOString())
  }
}

This package does not support any sort of date arithmetic, it simply handles labeling values as valid DateStr instances. If you are working with these DateStr types and need to perform any sort of parsing or manipulation, consider passing them into Luxon's DateTime.fromISO function. The resulting DateTime values can be manipulated and then converted back into a DateStr with Luxon's DateTime#toISODate function:

import { DateTime } from 'luxon';
import { toDateStr } from 'date-str';

const today = toDateStr(new Date()); // DateStr

const todayDateTime = DateTime.fromISO(today); // DateTime

const tomorrowDateTime = todayDateTime.plus({ days: 1 }); // DateTime

const tomorrow = toDateStr(tomorrowDateTime.toISODate()); // DateStr

API

isDateStr(str: unknown) => date is DateStr

Test if a value is a valid date string of the format YYYY-MM-DD.

toDateStr(date: string | Date) => DateStr

Parses a date string or JS Date object into a DateStr. Throws a TypeError if the date is not a valid value. If date is a string, it must be of the format YYYY-MM-DD.