At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Ruby – Date & Time Tutorial (With Examples)

  • By Karthiga M
  • December 31, 2019
  • 12069 Views

Date and Time are one of the basic requirement for any web application. Ruby on rails provides you with Date and time class helps you represent a specific point in time in your web application. Ruby has three components for representing Date and Time class.

  • Date – Date class manages the dates, day, month, year.
  • Time – It manages the seconds, minute, hours.
  • DateTime – It includes the above two classes. A DateTime object can be created with many subclasses like new, parse, strptime, now, commercial.

The Date Class

The date class managed everything in terms of day format. To use the date we have to initialize the Date class.

                   require 'date'

We can get the current date by using the method below.

                   Date.today

Date class is incorporated with the arithmetic class to manipulate the days, weeks and years. For example, the date object (current date or any date) with any number of days, weeks or years can be added. The result will be displayed as you expect. You can see the image below, I think you could understand better with it.

We can initialize a date object with a ‘new’ method. But It won’t return to its current date. It produces a negative date

Date.new

Date with its subclasses:

1. Date Parsing

It converts a string to a date object. The heuristic algorithm used to detect an input whether date or not. You can see the below example for a better understanding.

With rails, we can get many features like tomorrow, yesterday, beginning of the week, etc. If you want to know more, please check this link: Click here

2. Strptime

This class is for converting an object to date format as we like. We have to give the format with string quotes.

If you want more details, refer to this document by clicking here.

The Time Class

All the “Time” details are stored in Time class. It includes hour, minute, seconds. Time class follows UNIX time.
By default, UTC time format used in it. If we want to change the time format we can change by using config.time_zone.
For example, we can include these settings in config/application.rb file below like this.

config.time_zone = ‘Eastern Time (US & Canada)’

utc_offset method is used for offset in time objects. This will return the result in seconds. If you want to offset in hours you can divide the result by 3600.

To initialize the time object we can use Time.now. This will return output as a time object in the format of hours, seconds, or minutes.
we can know which second, minute, hour via below comments.

t = Time.now
t.day
t.month
t.hour

In addition, we can know the day’s details also.

t = Time.now
t.monday?
t..tuesday?

This will return the boolean value as a result. The boolean value should be either true or false.

Time Class with its subclasses:

1. Date Parsing
It converts the string representation of time into time object.

If we give only time, It will return only the current date with the time which we mention it in the input.

2. Strptime

It works similar to date class strptime. We must require ‘time’ to use this method.

3. xmlschema(date)

Parse date as a DateTime object defined by the XML schema and it converts the time object. If the date is not in the format, it will trigger argument errors.

4. Zoneoffset

Returns the number of seconds that the specified time zone differs from UTC. If we mention the time_zone configuration in config/application.rb as it is, Rails consider the default time zone that is mentioned in the configuration.
When the time zone mismatches with any of the above time classes, zone_offset will check if the local time zone (both with and without potential Daylight Saving Time changes being in effect) matches zone. Specifying a value for the year will change the year used to find the local time zone.

This article helps you out, how to use time and date object and Activesupport time extensions.
Get in touch with us to build a world-class web application at affordable prices by hiring the most skilled developers in the tech industry. Contact now.

Looking for a Tech partner to dominate the digital world?

 

Karthiga M

A desired and an ideal full stack developer having 2 years of experience in web development, Acquiring strong development skills in building Ruby On Rails and React Js applications.