Ruby Date and Time

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

time1 = Time.new
puts "Current Time : " + time1.inspect

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to getting Date and Time
time = Time.new
   
# Components of a Time
puts "Current Time :"+ time.inspect
puts 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 Sunday
puts time.yday    # => 365: Day of year
puts time.hour    # => 23: 24-hour clock
puts time.min     # => 59
puts time.sec     # => 59
puts time.usec    # => 999999: microseconds
puts time.zone    # => "UTC": timezone name

chevron_right


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:

  1. Calendar date: The calendar date is specific day of a calendar.
  2. Ordinal date: The ordinal date is a specific day of a calendar year.
  3. Week date: The week date is a date which represent week in the calendar.
  4. 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 ).
  5. 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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate different methods of date
  
# require ‘date’ is use to print date on screen
require '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 date
puts Date.ordinal(2018,15)    
puts Date.new(2018,4,5

chevron_right


Output:

1999-07-17
2019-01-29
2019-04-06
2018-08-07
2018-01-15
2018-04-05

Time and date Directives:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.