Giter Site home page Giter Site logo

kugland / parse-date-regex Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 10 KB

Regex that validates and parses dates in the format YYYY-MM-DD.

License: MIT License

Perl 100.00%
date-validator javascript dates regex regexp regex-validator regex-pattern

parse-date-regex's Introduction

parse-date-regex

This regex validates and parses dates in the format YYYY-MM-DD, taking into account leap years and the number of days in each month. Leading zeros are discarded in the captures. Accepts dates with year in range [0, 9999].

By removing the spaces and comments in the regex, it can be used in Javascript or any other language that supports PCRE (Perl-compatible regular expressions), e.g.:

const parseDate = /^0{0,3}(?<year>(?<=^000)\d|(?<=^00)[1-9]\d|(?<=^0)[1-9]\d\d|(?<=^)[1-9]\d{3})-0?(?<month>(?<=0)[1-9]|(?<=-)1[0-2])-0?(?<day>(?<=0)[1-9]|(?<=-)(?:1\d|2[0-8])|(?<!02-)(?:29|30)|(?<=0[13578]-|1[02]-)31|(?<=(?:[02468][048]|[13579][26])00-02-|\d\d(?:0[48]|[2468][048]|[13579][26])-02-)29)$/;

const { year, month, day } = '1985-02-02'.match(parseDate).groups;
console.info({ year, month, day });
// => { year: '1985', month: '2', day: '2' }
console.info('1900-02-29'.match(parseDate));
// => null

The Perl script also includes a test for the regex.

parse-date-regex's People

Contributors

kugland avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.