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

iso-datestring-validator

v2.2.2

Published

The goal of the package is to provide lightweight tools for validating strings denotings dates and time. It includes ISO 8601 datestring validation, simple YYYY-MM-DD date validation and time validation in hh:mm:ss.fff format. See details in readme.

Downloads

207,107

Readme

iso-datestring-validator

What is it

A simple package for validating strings denoting dates and time, including ISO 8601 format. The package provides the following functions:

  1. Date validation. YYYY-MM-DD format from 0001-01-01 to 9999-12-31, leap year friendly. Custom digit separators and null separators supported: YYYY/MM/DD or YYYYMMDD is no problem.

  2. Time validation. HH:mm:ss.fff±hh:mm format, seconds, fractions of seconds and timezone offset being optional. Custom digit separators supported for HHmmss as well (no custom separator for fractions, it is dot).

Caveat: do not use '-' and '+' as separators when validating time with timezone. I am reluctant to fix this unless it is an issue.

isValidTime('14-45-15.000+00-00', '-', true);
// will yield wrong result
  1. Year-month validation.

  2. ISO 8601 datestring validation with timezones, with and without separators:

  • 2019-07-09T15:03:36.000+00:00
  • 2019-07-09T15:03:36Z
  • 20190709T150336Z

Installation

npm i --save iso-datestring-validator

or

yarn add iso-datestring-validator

Import

import * as isoDatestringValidator from 'iso-datestring-validator';

alternatively you can import the function that you need separately:

import {
  isValidDate,
  isValidISODateString,
  isValidTime,
  isValidYearMonth,
} from 'iso-datestring-validator';

Usage

Date validation

Pass a YYYY-MM-DD date string to the isValidDate function to check it. To validate dates that use a custom digit separator, pass it as the second argument.

import { isValidDate } from 'iso-datestring-validator';

isValidDate('2019-01-31');
// true

isValidDate('20190131');
// false, no custom digit separator provided, hyphen separator not found in the string

isValidDate('20190131', '');
// true

isValidDate('2019/01/31', '/');
// true

Time validation

Time string in HH:mm:ss.fff±hh:mm format can be validated with the isValidTime function. Seconds and fractions are optional. However, if using fractions min number of numbers is 1 and max is 9. Zone offset is optional as well, its check is switched off by default.

import { isValidTime } from 'iso-datestring-validator';

isValidTime('13:00');
// true

isValidTime('13:00:00');
// true

isValidTime('13:00:00.000000000');
// true

// pass time, separator and boolean flag to enable zone offset check
isValidTime('14:45:15.000+00:00', ':', true);
// true

// you can take advantage of default separator if you pass undefined as second param
isValidTime('14:45:15.000+00:00', undefined, true);
// true

isValidTime('144515.000Z', '', true);
// true

Year and month validation

These are validated by the isValidYearMonth function. Rules same as in the previous case: a string YYYY-MM and a custom digit separator if required.

import { isValidYearMonth } from 'iso-datestring-validator';

isValidYearMonth('2019/01', '/');
// true

isValidYearMonth('2019-01');
// true

ISO 8601 datestring validation

Pass a string to isValidISODateString to see if it is valid.

import { isValidISODateString } from 'iso-datestring-validator';

isValidISODateString('2019-07-09T15:03:36.000+00:00');
// true

isValidISODateString('20190709T150336Z');
// true

That's all about this package. Have fun, feel free to contribute with some test :]

"Buy Me A Coffee"