Ruby has a class called Time, which holds a range of information regarding the date and time. To access the current date and time you can assign a new instance of the time class to a variable like so:
Below is a screen-recording to show how the Time class can be accessed within the console, and what it is doing:
You can access different components in the Time class with different key words:
Formatting the Date and Time
For formatting the date and time we can use our method friend .strftime and his directives.
.strftime stands for “standard formatting time” and below you can find a list of what are called directives.
Directives specify what time information we want in a neat and concise way. For instance:
See how both methods return the same result, but .strftime makes writing the action much neater and quicker?
A list of Directives for Standard Formatting
If you would like to find some more methods that act upon the Time class, you can refer to the Ruby Docs on this. There is also very good information at Tutorials Point.
Regarding that screen-recording…
In this video I wrote out the time formatting with its directives in an incredibly long winded way.
It wrote it like: