Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Components of Dates and Times in Ruby
3.
Formatting of Date in Standard Form
4.
Timezones and Daylight Savings Time
5.
Date and Time directives.
6.
Frequently Asked Questions
6.1.
What is the difference between strptime and strftime?
6.2.
How do we parse Time in ruby?
6.3.
When do we use the DateTime class?
6.4.
How can we get Today's date in ruby?
6.5.
What do you understand by the term Time and DateTime in ruby?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dates and Times in Ruby

Introduction

The Time class in ruby represents dates and times. The time layer is thin over the operating system's system date and time functionality. On some platforms, therefore, this class may be unable to represent dates before 1970 or after 2038.

The Date means the Day of a month or year represented by a number. The Date consists of a month, year, and Date.

In this article, we will discuss dates and times in ruby.

Dates and Times in Ruby

Let's see an example of how we can get the current dates and times in ruby.

Input:

t=Times.now
Puts “Current Time :” + t.inspect
You can also try this code with Online Ruby Compiler
Run Code

 

Output: 

Sat July 02 01:36:45 -0700 2022
You can also try this code with Online Ruby Compiler
Run Code

 

inspect function is used to get the current Date and Time in ruby.

We can also use Times.new instead of Times.now as new and now are the synonyms.

Components of Dates and Times in Ruby

The Time object is used to get different Date and Time components.

time = Time.new  
# Components of Dates and Times in ruby
puts "Current Time :"+ time.inspect
puts time.year   # => It gives Year of the date 
puts time.month  # => It gives Month of the date (1 to 12)
puts time.day    # => It gives Day of the date (1 to 31 )
puts time.wday   # => It means Day of week: 0 is Sunday
puts time.yday   # => It means Day of the Year: 365
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
You can also try this code with Online Ruby Compiler
Run Code


Output

Current Time: Sat July 02 04:08:27 -0700 2022
2022
7
2
6
183
4
8
27
864801
UTC
You can also try this code with Online Ruby Compiler
Run Code

Formatting of Date in Standard Form

Time.local, Time.gm, Time.utc format the Date in standard form.
 Let us see some examples.

Input:

puts Time.local(2022, 7, 2)
You can also try this code with Online Ruby Compiler
Run Code

 

Output: 

July 2 2022
You can also try this code with Online Ruby Compiler
Run Code

 

Input:

puts Time.local(2022, 7, 2, 9, 10)
You can also try this code with Online Ruby Compiler
Run Code

 

Output: 

July 2 2022 9:10
You can also try this code with Online Ruby Compiler
Run Code

 

Input:

puts Time.gm(2022, 7, 2, 9, 10, 11)
You can also try this code with Online Ruby Compiler
Run Code

 

Output: 

July 2 2022 9:10:11
You can also try this code with Online Ruby Compiler
Run Code

 

Input:

puts Time.utc(2022, 7, 2, 9, 10)
You can also try this code with Online Ruby Compiler
Run Code

 

Output: 

July 2 2022 9:10
You can also try this code with Online Ruby Compiler
Run Code


The Format to get all components in an array is - 

[sec,min,hour,day,month,year,wday,yday,isdst,zone]
You can also try this code with Online Ruby Compiler
Run Code

 

Input:

t = Time.new
values = t.to_a
p values
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

[27, 8, 4, 2, 7, 2022, 6, 183, false, "UTC"]
You can also try this code with Online Ruby Compiler
Run Code

 

Input:

values[5] += 1     # Increment the year 
puts Time.utc(*values)
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

Sat July 2 4:8:27 UTC 2022
You can also try this code with Online Ruby Compiler
Run Code

Timezones and Daylight Savings Time

We can use a Time object to get all the information related to Timezones and daylight savings as follows −

t = time.new
t.zone  # => "UTC": return the timezone 
t.utc?  # => true: t is in UTC time zone 
t.utc_offset # => 0: UTC is 0 seconds offset from UTC 
t.localtime  # Convert to local timezone. Mutates the Time object! 
t.zone  # => "PST" (or whatever your timezone is) 
t.utc?  # => false t.utc_offset # => -28800: 8 hours before UTC 
t.gmtime  # Convert back to UTC. Another mutator. 
t.getlocal  # Return a new Time object in local zone 
t.getutc  # Return a new Time object in UTC 
t.isdst  # => false: UTC does not have DST. Note no ?. 
t.getlocal.isdst  # => false: no daylight savings time in winter
You can also try this code with Online Ruby Compiler
Run Code

Date and Time directives.

The various directives that are used in the strftime method are.

  • %a:  The name of the Weekday (for example, Sat ).
  • %A:  The full name of Weekday (for example, Saturday).
  • %b:  The name of the Month (for example, Jan)
  • %B:  The full name of the Month (for example, January )
  • %c:  It represents the selected local Time and Date.
  • %d:  It means Day of the Month (1 to 31).
  • %H:  It represents a 24-hour clock
  • %I:  It represents a 12-hour clock
  • %j:  It indicates the Day of the year
  • %m: It represents the Month of the year
  • %M:  It shows Minute
  • %p:  It means Meridian indicator
  • %S:  It means Seconds
  • %%:  % literal
  • %z:  Name of the time zone.
  • %Y:  Name of the year with the century
  • %y:  Name of the year without the century
  • %X:  It represents the selected Time only.
  • %x:  It means the specified date only.
  • %w:  It means Weekday
  • %U:  It indicates the week number of the current year, starting with the first Sunday
  • %W:  It means the week number of the current year, beginning with the first Monday

Frequently Asked Questions

In the above, we have different topics of dates and times in ruby. Now let us see some FAQs related to them.

What is the difference between strptime and strftime?

strptime is a string parser that converts a string format to DateTime, whereas strftime is a string formatter that formats a DateTime object to string format.

How do we parse Time in ruby?

We can use the DateTime parse() function to parse the Time in ruby.

When do we use the DateTime class?

We use the DateTime class if we need both the Date and Time.

How can we get Today's date in ruby?

The today method creates the date object from the current Date or today date in Ruby language. To get Today's date, use Date.today method.

What do you understand by the term Time and DateTime in ruby?

Time in ruby is an object. It uses UNIX time, which counts the number of seconds from Jan 1, 1970 (the UNIX epoch), but DateTime is a calendar-based approach that stores the year, month, Day, Hour, Minute, and second individually.

Conclusion

We have discussed the concept of the dates and times in ruby. We also discussed different components in dates and times in ruby.

After reading about the dates and times in ruby, are you not feeling excited to read/explore more articles on various CMS Platforms? Don't worry; Coding Ninjas has you covered. See RubyIntroduction in RubyRuby on Rails- MigrationsMethods in Ruby, and Learn Ruby. You can also refer to the Official RubyRubyOfficial DocumentationRuby FAQ, and Ruby Koans.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image

Live masterclass