Hey guys. Hello again. Hello, dear readers, followers, coders and enthusiasts. Welcome to the next article about Ruby PL. And this time we are going to learn about Date and Time stuff in Ruby.
Methods for working with Date and Time (date + hour/minute/second) are really important in all the programming languages. They have to be easy to use and clear. In Ruby, there are 3 different classes for this. They are :
- Date (only date)
- DateTime (date & time)
- Time (date & time)
Now, let's take a look at each of them in detail.
This class is used for defining date and time. It means, it's content is composed of two parts, date and time.
- Date part : day (month day) / month / year
- Time part : hours / minutes / seconds
These parts, keep the number of seconds passed since so-called "Epoch time", or otherwise called the “Unix Time”. “Unix Time” is actually accepted as “00:00:00 UTC on 1 January 1970”. So, it keeps the number of seconds passed since 1-st of January 1970 till the date that you defined.
Let's show some examples to demonstrate what this class actually returns.
- Time.now => current time and date
- Time.new => time & date given as a parameter
- Time.at => the date, that corresponds to the number of seconds (given as a parameter) passed since 1-st of January 1970.
Time.now # 2020-08-02 17:43:04 +0500 Time.new(2020, 8, 2) # 2020-08-02 00:00:00 +0500 Time.at(15000000000) # 2445-05-01 07:40:00 +0500
You can even output separate parts of it on the screen. All you have to do is ask for it from the "Time" class, using its built-in methods. For instance:
# current day, month, hour t = Time.now puts t.day puts t.month puts t.hour
Ruby even has built-in methods to check what weekday it is. It is a savior set of methods for a Ruby developer. Ruby has built-in methods named the same as weekdays for this. Methods used below return "true" or "false" depending on the day of the week.
t = Time.now # Current time & date puts t.monday? # false puts t.sunday? # true puts t.friday? # false
“Date” class has nothing to do with time, just date. It has no information about the current hour, minute, and second. It helps only to work with data related to date. Here are some examples:
Date.today # Today's date # Returns negative value. # Because no value is given as a parameter. Date.new # Adding 1 day to today's date # because all calculations are performed in terms of days. Date.today + 1
It is a subclass of the "Date" class actually. This class, as its name states, contains information about the date and the time.
Let me answer a question that can arise immediately in your head. Then what is the difference between “DateTime” and “Time” classes?
According to the resources, both “DateTime” and “Time” classes can be used for the same purpose. It seems like there is absolutely no difference. But the thing is, that “Time” class is developed with “C” language, meaning that it works faster. For instance, here is the speed comparison of time calculations:
Comparison: Time: 2644596.6 i/s DateTime: 231634.8 i/s - 11.42x times slower
The method used to print out date and time in an easily understandable format is “strftime”. It gives the opportunity to print all day, month, year, hour, minute, second in a customized and formatted way.
time = Time.new time.strftime("%d/%m/%Y") # "05/12/2015" time.strftime("%k:%M") # "17:48" time.strftime("%I:%M %p") # "11:04 PM" time.strftime("Today is %A") # "Today is Sunday" time.strftime("%d of %B, %Y") # "21 of December, 2015" time.strftime("Unix time is %s") # "Unix time is 1449336630"
There are special symbols to use to format date and time in your customized format. You don't have to memorize them. You can just open the table and find the one you need at that time. Here they are:
- %d - day of the month (01..31)
- %m - month (01..12), use %-m to format like (1–12)
- %k - hour (0..23)
- %M - minutes
- %S - seconds (00..60)
- %I - hours (1..12)
- %p - AM/PM
- %Y - year
- %A - name of the weekday
- %B - month name
Here you can see primary formatting symbols that are used the most. You can find detailed information on the website: strftime.
We came to an end of our article about time and date classes in Ruby. Now, your toolset of Ruby PL is even richer. So you can write more complicated code. I truly hope this article will help you achieve more in your Ruby code.
So don't stop. Learn and code. Read and implement! Good Luck!