Updated April 3, 2023
Introduction to Ruby DateTime
Everything in Ruby is class and object, so here DateTime is also available with the help of the date class, here DateTime is a subclass of the date, because DateTime is a subclass of the Date it allow us to easily deal with hour, minute, second and even it allow us to get the offset value. There is a drawback of the DateTime which is we can not handle or simply we can say, DateTime has not considered the leap element , which means it will not consider the leap hour, leap minute and leap second.
Syntax
DateTime can have many types of the syntax. A syntax if we want to get output till second and upto offset. In the below syntax we can see that we have required the date module and in the DateTime we are passing the year, month, day, hours and second . A good thing about this format here we can use fraction also.
require 'date'
DateTime.new(year, month, day, hours, second)
How DateTime works in Ruby?
Given below is the working:
- DateTime is a subclass of Date, we have to require the date to use the DateTime feature.
- One important thing about the DateTime, it will not handle or take in consideration the leap second, hour, minute and offset.
- To create any object for DateTime we will use the new. We can pass the arguments in the new for DateTime and the output will be in front of us.
Various DateTime Functions in Ruby
Given below are the various functions:
1. new
This is the most important and highly used function over DateTime. Anything like converting one date format to another will be easily done with the help of this attribute.
Example
- First we have required the main module of the DateTime, as every DateTime is a subclass of the date.
- Finally inside the new function we are passing some arguments with comma separated, these arguments are for year, month, date , hour , minute, second and offsets .
- Finally, we are printing the output of the expression and we got the date value.
Code:
require 'date'
date = DateTime.new(2020,3,4,5,6,7)
puts "The date value is #{date}"
Output:
2. parse
With the help of parse function we can convert the string date format, it may be nay format (we can read all the available formats for the ruby date) and allow us to get hour, minute, second and even offset value, it is also possible to get the time zone with help of this function.
Example:
- First we have required the main module of the DateTime, as every DateTime is a subclass of the date .
- In the second step we have passed the date string format to the function parse.
- Next step we are getting the value of the hour, minute, second and offset value into some variables .
- Finally we are displaying the value of these variables.
Code:
require 'date'
date = DateTime.parse('3rd Mar 2020 04:05:06+03:30')
hourValue= date.hour
minuteValue =date.min
secondValue =date.sec
offsetvalue =date.offset
zonevalue =date.zone
puts "The hour value is #{hourValue}"
puts "The minute value is #{minuteValue}"
puts "The second value is #{secondValue}"
puts "The offset value is #{offsetvalue}"
puts "The zone value is #{zonevalue}"
Output:
3. rfc2822
This function will be used to convert string of date format to some RFC 2822 formats.
Example:
- First we have required the main module of the DateTime, as every DateTime is a subclass of the date.
- And in the final we are passing a string of date format to it and it will give the date as the output from this.
Code:
require 'date'
dateoutput = DateTime.rfc2822('Sat, 3 Feb 2001 04:05:06 +0700')
puts dateoutput
Output:
4. strptime
This is one of the most useful functions. It used to convert the string of date format to a DateTime object and as we will convert it into a DateTime object we are allowed to perform all the date operations on it.
Example:
Code:
require 'date'
dateoutput1 = DateTime.strptime('2020-03-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z')
puts dateoutput1
dateoutput2 =DateTime.strptime('03-02-2020 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p')
puts dateoutput2
Output:
5. hour, min, sec, offset
Here we have used four functions altogether as all of them are very much similar in behavior . We have used the function hour, minute, sec and offset on the Date object which was created by passing date in the form of comma separated values as the year, month, hour , minute second and offset values.
Example:
Code:
require 'date'
hour = DateTime.new(2020,3,4,5,6,7).hour
minute = DateTime.new(2020,3,4,5,6,7).min
second = DateTime.new(2020,3,4,5,6,7).sec
offset = DateTime.new(2020,3,4,5,6,7).offset
puts "The hour value from the date attribute is #{hour}"
puts "The minute value from the date attribute is #{minute}"
puts "The second value from the date attribute is #{second}"
puts "The offset value from the date attribute is #{offset}"
Output:
Conclusion
From these tutorials we learned some important uses and basic concepts of DateTime in Ruby with the help of some useful examples, we also learned the working of DateTime with the help of syntax, here we have given little focus on the uses of DateTime in real world cases.
Recommended Articles
We hope that this EDUCBA information on “Ruby DateTime” was beneficial to you. You can view EDUCBA’s recommended articles for more information.