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 Ruby, Introduction in Ruby, Ruby on Rails- Migrations, Methods in Ruby, and Learn Ruby. You can also refer to the Official Ruby, Ruby, Official Documentation, Ruby FAQ, and Ruby Koans.
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!