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.
npm install iso-datestring-validatorjs
isValidTime('14-45-15.000+00-00', '-', true);
// will yield wrong result
`
3. Year-month validation.
4. 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
`js
npm i --save iso-datestring-validator
`
or
`js
yarn add iso-datestring-validator
`
Import
`ts
import * as isoDatestringValidator from 'iso-datestring-validator';
`
alternatively you can import the function that you need separately:
`ts
import {
isValidDate,
isValidISODateString,
isValidTime,
isValidYearMonth,
} from 'iso-datestring-validator';
`
Usage
$3
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.
`ts
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
`
$3
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.
`ts
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
`
$3
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.
`ts
import { isValidYearMonth } from 'iso-datestring-validator';
isValidYearMonth('2019/01', '/');
// true
isValidYearMonth('2019-01');
// true
`
$3
Pass a string to isValidISODateString to see if it is valid.
`ts
import { isValidISODateString } from 'iso-datestring-validator';
isValidISODateString('2019-07-09T15:03:36.000+00:00');
// true
isValidISODateString('20190709T150336Z');
// true
``