Ruby Date and Time - GeeksforGeeks

06 Sep, 2019
The class Time and Date in Ruby deals with the dates and times. It helps us to get the current time and date according to our system configurations. It also gives us the components of a date and time and also to format time and date.The date is the day of a month or year that is represented by a number. Date consists of a month, year, date. The date objects are created with ::new, ::parse, ::today, ::jd, ::strptime, ::ordinal, ::commercial etc. All the date objects are unchangeable.
Getting current date and time.Example :
time1 = Time.newputs "Current Time : " + time1.inspect
Output
Current Time : 2019-09-05 04:09:21 +0000
In above example inspect function is used to get current date and time.
Getting Components of Date and Time.
The Time object can be used to get various components of Date and Time.Example :
# Ruby program to getting Date and Timetime = Time.new   # Components of a Timeputs "Current Time :"+ time.inspectputs time.year    # => Year of the date puts time.month   # => Month of the date (1 to 12)puts time.day     # => Day of the date (1 to 31 )puts time.wday    # => 0: Day of week: 0 is Sundayputs time.yday    # => 365: Day of yearputs time.hour    # => 23: 24-hour clockputs time.min     # => 59puts time.sec     # => 59puts time.usec    # => 999999: microsecondsputs time.zone    # => "UTC": timezone name
Output
Current Time :2019-09-05 04:08:27 +0000
2019
9
5
4
248
4
8
27
864801
UTC
There are following terms used in date:
Calendar date: The calendar date is specific day of a calendar.Ordinal date: The ordinal date is a specific day of a calendar year.Week date: The week date is a date which represent week in the calendar.Julian day number: The Julian day number is in passed day since noon(Greenwich Mean Time) on January 1,4713 BCE(in the Julian calendar ).Modified Julian day number: The Julian day number is in passed day since midnight(Coordinated universal Time) on November 17,1858 CE(in the Gregorian calendar).
Calendar date: The calendar date is specific day of a calendar.
Ordinal date: The ordinal date is a specific day of a calendar year.
Week date: The week date is a date which represent week in the calendar.
Julian day number: The Julian day number is in passed day since noon(Greenwich Mean Time) on January 1,4713 BCE(in the Julian calendar ).
Modified Julian day number: The Julian day number is in passed day since midnight(Coordinated universal Time) on November 17,1858 CE(in the Gregorian calendar).
Below is the example to get date.Example :
# Ruby program to illustrate different methods of date  # require ‘date’ is use to print date on screenrequire 'date'   # print Julian day number           puts Date.jd(2451377)     # print commercial date  puts Date.commercial(2019,5,2)     puts Time.new(2019,4,6).to_date     puts Date.strptime('07-08-2018', '%d-%m-%Y')   # print ordinal dateputs Date.ordinal(2018,15)    puts Date.new(2018,4,5) 
Output:
1999-07-17
2019-01-29
2019-04-06
2018-08-07
2018-01-15
2018-04-05
The directives used in the strftime method are:
%a: The abbreviated weekday name (for example: Sun ).
%A: The full name of the weekday (for example: Sunday).
%b: The abbreviated name of the month(for example: Jan)
%B: The full name of the month(for example: January )
%c: The selected local date and time representation
%d: Day of the month (1 to 31).
%H: 24-hour clock
%I: 12-hour clock
%j: day of the year
%m: month of the year
%M: minute
%p: meridian indicator
%S: seconds
%%: % literal
%z: time zone name
%Y: year name with the century
%y: year name without the century
%X: selected representation of time only
%x: selected representation of date only
%w: weekday
%U: week number of current year, starting with first Sunday
%W: week number of current year, starting with first Monday
Picked
Ruby-Methods
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Include v/s Extend in Ruby
Global Variable in Ruby
Ruby | Hash delete() function
Ruby | Types of Variables
Ruby | Enumerator each_with_index function
Ruby | Case Statement
Ruby | Array select() function
Ruby | Data Types
Ruby | Numeric round() function
Ruby | String capitalize() Method