Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
Date.parse
(?::String str, ?::boolish complete, ?::Integer start) -> ::Date
Date.parse(string = '-4712-01-01', comp = true, start = Date::ITALY, limit: 128) → date
ext/date/date_core.c
View on GitHub ↗
static VALUE
date_s_parse(int argc, VALUE *argv, VALUE klass)
{
VALUE str, comp, sg, opt;
argc = rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt);
switch (argc) {
case 0:
str = rb_str_new2(JULIAN_EPOCH_DATE);
case 1:
comp = Qtrue;
case 2:
sg = INT2FIX(DEFAULT_SG);
}
{
int argc2 = 2;
VALUE argv2[3], hash;
argv2[0] = str;
argv2[1] = comp;
if (!NIL_P(opt)) argv2[argc2++] = opt;
hash = date_s__parse(argc2, argv2, klass);
return d_new_by_frags(klass, hash, sg);
}
}
Note: This method recognizes many forms in string, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times If string does not specify a valid date, the result is unpredictable; consider using Date._strptime instead.
Returns a new Date object with values parsed from string:
Example
Date.parse('2001-02-03') # => #<Date: 2001-02-03> Date.parse('20010203') # => #<Date: 2001-02-03> Date.parse('3rd Feb 2001') # => #<Date: 2001-02-03>
If comp is true and the given year is in the range (0..99), the current century is supplied; otherwise, the year is taken as given:
Example
Date.parse('01-02-03', true) # => #<Date: 2001-02-03> Date.parse('01-02-03', false) # => #<Date: 0001-02-03>
See:
Related: Date._parse (returns a hash).