Synonym for $stdin.
Synonym for $stdout.
Returns a new Date object formed fom the arguments.
With no arguments, returns the date for January 1, -4712:
Date.ordinal.to_s # => "-4712-01-01"
With argument year
, returns the date for January 1 of that year:
Date.ordinal(2001).to_s # => "2001-01-01" Date.ordinal(-2001).to_s # => "-2001-01-01"
With positive argument yday
== n
, returns the date for the nth
day of the given year:
Date.ordinal(2001, 14).to_s # => "2001-01-14"
With negative argument yday
, counts backward from the end of the year:
Date.ordinal(2001, -14).to_s # => "2001-12-18"
Raises an exception if yday
is zero or out of range.
See argument start.
Returns a new Date object constructed from the present date:
Date.today.to_s # => "2022-07-06"
See argument start.
Returns a hash of values parsed from string
according to the given format
:
Date._strptime('2001-02-03', '%Y-%m-%d') # => {:year=>2001, :mon=>2, :mday=>3}
For other formats, see Formats for Dates and Times. (Unlike Date.strftime
, does not support flags and width.)
See also strptime(3).
Related: Date.strptime
(returns a Date object).
Returns a new Date object with values parsed from string
, according to the given format
:
Date.strptime('2001-02-03', '%Y-%m-%d') # => #<Date: 2001-02-03> Date.strptime('03-02-2001', '%d-%m-%Y') # => #<Date: 2001-02-03> Date.strptime('2001-034', '%Y-%j') # => #<Date: 2001-02-03> Date.strptime('2001-W05-6', '%G-W%V-%u') # => #<Date: 2001-02-03> Date.strptime('2001 04 6', '%Y %U %w') # => #<Date: 2001-02-03> Date.strptime('2001 05 6', '%Y %W %u') # => #<Date: 2001-02-03> Date.strptime('sat3feb01', '%a%d%b%y') # => #<Date: 2001-02-03>
For other formats, see Formats for Dates and Times. (Unlike Date.strftime
, does not support flags and width.)
See argument start.
See also strptime(3).
Related: Date._strptime
(returns a hash).
Returns the Julian start date for calendar reform; if not an infinity, the returned value is suitable for passing to Date#jd
:
d = Date.new(2001, 2, 3, Date::ITALY) s = d.start # => 2299161.0 Date.jd(s).to_s # => "1582-10-15" d = Date.new(2001, 2, 3, Date::ENGLAND) s = d.start # => 2361222.0 Date.jd(s).to_s # => "1752-09-14" Date.new(2001, 2, 3, Date::GREGORIAN).start # => -Infinity Date.new(2001, 2, 3, Date::JULIAN).start # => Infinity
See argument start.
Calls the block with specified dates; returns self
.
The first date
is self
.
Each successive date
is date + step
, where step
is the numeric step size in days.
The last date is the last one that is before or equal to limit
, which should be a Date object.
Example:
limit = Date.new(2001, 12, 31) Date.new(2001).step(limit){|date| p date.to_s if date.mday == 31 }
Output:
"2001-01-31" "2001-03-31" "2001-05-31" "2001-07-31" "2001-08-31" "2001-10-31" "2001-12-31"
Returns an Enumerator
if no block is given.
Equivalent to step
with arguments min
and -1
.
Returns a string representation of the date in self
in ISO 8601 extended date format ('%Y-%m-%d'
):
Date.new(2001, 2, 3).to_s # => "2001-02-03"
Returns a string representation of the date in self
, formatted according the given format
:
Date.new(2001, 2, 3).strftime # => "2001-02-03"
For other formats, see Formats for Dates and Times.
Creates a DateTime
object denoting the given ordinal date.
DateTime.ordinal(2001,34) #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...> DateTime.ordinal(2001,34,4,5,6,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.ordinal(2001,-332,-20,-55,-54,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
Parses the given representation of date and time with the given template, and returns a hash of parsed elements. _strptime does not support specification of flags and width unlike strftime.
See also strptime(3) and strftime
.
Parses the given representation of date and time with the given template, and creates a DateTime
object. strptime does not support specification of flags and width unlike strftime.
DateTime.strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('03-02-2001 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p') #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...> DateTime.strptime('2001-W05-6T04:05:06+07:00', '%G-W%V-%uT%H:%M:%S%z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('2001 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('2001 05 6 04 05 06 +7', '%Y %W %u %H %M %S %z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('-1', '%s') #=> #<DateTime: 1969-12-31T23:59:59+00:00 ...> DateTime.strptime('-1000', '%Q') #=> #<DateTime: 1969-12-31T23:59:59+00:00 ...> DateTime.strptime('sat3feb014pm+7', '%a%d%b%y%H%p%z') #=> #<DateTime: 2001-02-03T16:00:00+07:00 ...>
See also strptime(3) and strftime
.
Returns a string in an ISO 8601 format. (This method doesn’t use the expanded representations.)
DateTime.new(2001,2,3,4,5,6,'-7').to_s #=> "2001-02-03T04:05:06-07:00"
Returns a string representation of self
, formatted according the given +format:
DateTime.now.strftime # => "2022-07-01T11:03:19-05:00"
For other formats, see Formats for Dates and Times:
Works similar to parse
except that instead of using a heuristic to detect the format of the input string, you provide a second argument that describes the format of the string.
Raises ArgumentError
if the date or format is invalid.
If a block is given, the year described in date
is converted by the block. For example:
Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
Below is a list of the formatting options:
The abbreviated weekday name (“Sun”)
The full weekday name (“Sunday”)
The abbreviated month name (“Jan”)
The full month name (“January”)
The preferred local date and time representation
Century (20 in 2009)
Day of the month (01..31)
Date (%m/%d/%y)
Day of the month, blank-padded ( 1..31)
Equivalent to %Y-%m-%d (the ISO 8601 date format)
The last two digits of the commercial year
The week-based year according to ISO-8601 (week 1 starts on Monday and includes January 4)
Equivalent to %b
Hour of the day, 24-hour clock (00..23)
Hour of the day, 12-hour clock (01..12)
Day of the year (001..366)
hour, 24-hour clock, blank-padded ( 0..23)
hour, 12-hour clock, blank-padded ( 0..12)
Millisecond of the second (000..999)
Month of the year (01..12)
Minute of the hour (00..59)
Newline (n)
Fractional seconds digits
Meridian indicator (“AM” or “PM”)
Meridian indicator (“am” or “pm”)
time, 12-hour (same as %I:%M:%S %p)
time, 24-hour (%H:%M)
Number of seconds since 1970-01-01 00:00:00 UTC.
Second of the minute (00..60)
Tab character (t)
time, 24-hour (%H:%M:%S)
Day of the week as a decimal, Monday being 1. (1..7)
Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
VMS date (%e-%b-%Y)
Week number of year according to ISO 8601 (01..53)
Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
Day of the week (Sunday is 0, 0..6)
Preferred representation for the date alone, no time
Preferred representation for the time alone, no date
Year without a century (00..99)
Year which may include century, if provided
Time zone as hour offset from UTC (e.g. +0900)
Time zone name
Literal “%” character
date(1) (%a %b %e %H:%M:%S %Z %Y)
require 'time' Time.strptime("2000-10-31", "%Y-%m-%d") #=> 2000-10-31 00:00:00 -0500
You must require ‘time’ to use this method.
Returns the value of self
as integer Epoch seconds; subseconds are truncated (not rounded):
Time.utc(1970, 1, 1, 0, 0, 0).to_i # => 0 Time.utc(1970, 1, 1, 0, 0, 0, 999999).to_i # => 0 Time.utc(1950, 1, 1, 0, 0, 0).to_i # => -631152000 Time.utc(1990, 1, 1, 0, 0, 0).to_i # => 631152000
Returns the value of self
as a Float
number Epoch seconds; subseconds are included.
The stored value of self
is a Rational, which means that the returned value may be approximate:
Time.utc(1970, 1, 1, 0, 0, 0).to_f # => 0.0 Time.utc(1970, 1, 1, 0, 0, 0, 999999).to_f # => 0.999999 Time.utc(1950, 1, 1, 0, 0, 0).to_f # => -631152000.0 Time.utc(1990, 1, 1, 0, 0, 0).to_f # => 631152000.0
Returns the value of self
as a Rational
exact number of Epoch seconds;
Time.now.to_r # => (16571402750320203/10000000)
Returns a string representation of self
, without subseconds:
t = Time.new(2000, 12, 31, 23, 59, 59, 0.5) t.to_s # => "2000-12-31 23:59:59 +0000"
Related: Time#ctime
, Time#inspect
:
t.ctime # => "Sun Dec 31 23:59:59 2000" t.inspect # => "2000-12-31 23:59:59.5 +000001"
Returns a 10-element array of values representing self
:
Time.utc(2000, 1, 1).to_a # => [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"] # [sec, min, hour, day, mon, year, wday, yday, dst?, zone]
The returned array is suitable for use as an argument to Time.utc
or Time.local
to create a new Time
object.
Returns a new Time
object whose numerical value is less than or equal to self
with its seconds truncated to precision ndigits
:
t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r) t # => 2010-03-30 05:43:25.123456789 UTC t.floor # => 2010-03-30 05:43:25 UTC t.floor(2) # => 2010-03-30 05:43:25.12 UTC t.floor(4) # => 2010-03-30 05:43:25.1234 UTC t.floor(6) # => 2010-03-30 05:43:25.123456 UTC t.floor(8) # => 2010-03-30 05:43:25.12345678 UTC t.floor(10) # => 2010-03-30 05:43:25.123456789 UTC t = Time.utc(1999, 12, 31, 23, 59, 59) t # => 1999-12-31 23:59:59 UTC (t + 0.4).floor # => 1999-12-31 23:59:59 UTC (t + 0.9).floor # => 1999-12-31 23:59:59 UTC (t + 1.4).floor # => 2000-01-01 00:00:00 UTC (t + 1.9).floor # => 2000-01-01 00:00:00 UTC
Related: Time#ceil
, Time#round
.
Returns true
if self
is in daylight saving time, false
otherwise:
t = Time.local(2000, 1, 1) # => 2000-01-01 00:00:00 -0600 t.zone # => "Central Standard Time" t.dst? # => false t = Time.local(2000, 7, 1) # => 2000-07-01 00:00:00 -0500 t.zone # => "Central Daylight Time" t.dst? # => true