0.1.1 • Published 4 years ago

is-my-date-valid v0.1.1

Weekly downloads
2,627
License
MIT
Repository
github
Last release
4 years ago

is-my-date-valid

A date validator, with support for custom formats.

Installation

npm install --save is-my-date-valid

Usage

const validator = require('is-my-date-valid')
const validate = validator({ format: 'YYYY-MM-DD' })

validate('1992-12-02') // true

validate('1992/12/02') // false
validate('1992-18-02') // false

validate(['hello', 'world']) // TypeError: expected string

API

validator(opts)

Returns a new validator, supported options:

  • format: The format of the date, see the table below for valid tokens

validate(str)

Validate a date, returns true if the date is valid, false otherwise.

Format tokens

InputExampleDescription
YYYY20144 or 2 digit year
YY142 digit year
Y-25Year with any number of digits and sign
Q1..4Quarter of year
M MM1..12Month number
MMM MMMMJan..DecemberMonth name
D DD1..31Day of month
Do1st..31stDay of month with ordinal
DDD DDDD1..365Day of year
X1410715640.579Unix timestamp
x1410715640579Unix ms timestamp
gggg2014Locale 4 digit week year
gg14Locale 2 digit week year
w ww1..53Locale week of year
e0..6Locale day of week
ddd ddddMon...SundayDay name
GGGG2014ISO 4 digit week year
GG14ISO 2 digit week year
W WW1..53ISO week of year
E1..7ISO day of week
H HH0..2324 hour time
h hh1..1212 hour time used with a A.
a Aam pmPost or ante meridiem (Note the one character a p are also considered valid)
m mm0..59Minutes
s ss0..59Seconds
S SS SSS0..999Fractional seconds
Z ZZ+12:00Offset from UTC as +-HH:mm, +-HHmm, or Z