Ruby/Date/Date.new

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Calendar forms

# A date can conform to the Gregorian or Julian calendar by adding a fourth argument to new
require "date"
gdate = Date.new( 2006, 11, 8, "Gregorian" )
jdate = Date.new( 2006, 11, 8, "Julian" )



In England, 2 Sep as immediately followed by 14 Sep 1752.

require "date"
puts Date.new(1752, 9, 2, Date::ENGLAND).to_s



In Italy, 4 Oct as immediately followed by 15 Oct 1582.

require "date"
puts Date.new(1582, 10, 4).to_s
puts Date.new(1582, 10, 5).to_s
puts Date.new(1582, 10, 4).succ.to_s



The Date Class

# To create a Date object, use the new method (or its synonym civil). 
# You must require the Date class before using it. 
# The to_s method returns the date as a string.
require "date"
date = Date.new( 2006, 11, 8 )
date.to_s # => "2006-11-08"



Use expression substitution to show the date in a user-specified format.

# This line of code uses the month, day, and year methods from Date.
require "date"
date = Date.new( 2006, 11, 8 )
puts "The date was #{date.month}/#{date.day}/#{date.year}."