whatwg_datetime

Function parse_date

Source
pub fn parse_date(s: &str) -> Option<NaiveDate>
Expand description

Parse a proleptic-Gregorian date, in the format of YYYY-MM-DD

This follows the rules for parsing a date string per WHATWG HTML Standard § 2.3.5.2 Dates.

§Examples

use chrono::NaiveDate;
use whatwg_datetime::parse_date;

assert_eq!(parse_date("2011-11-18"), NaiveDate::from_ymd_opt(2011, 11, 18));
assert_eq!(parse_date("2012-02-29"), NaiveDate::from_ymd_opt(2012, 2, 29));
assert_eq!(parse_date("2007-02-29"), None); // 2007 is not a leap year
assert_eq!(parse_date("2011-00-19"), None); // invalid month
assert_eq!(parse_date("2012-11-1"), None);  // invalid day length, must be 2 digits/zero-padded
assert_eq!(parse_date("0000-11-02"), None); // invalid year, must be at least 0001