Handling of date and time in Ruby. Use Date and Time properly.

Introduction

Date class and Time class are used for date and time operations in Ruby. The Date class deals only with the date, and the Time class deals with the year, month, day, hour, minute, and second. I feel that I should use only the Time class, but since the Date class also has a strong field, I will explain how to use it properly.

table of contents

    1. Date is date, Time is year, month, day, hour, minute, second
  1. Date class is good at date processing
    1. Conclusion

1. 1. Date is date, Time is year, month, day, hour, minute, second

First, use Time if you want to display up to hours, minutes, and seconds, that is, if you want to handle the date and time. You can also use the DateTime class, which is a subclass of the Date class, but considering that you have to require'date', daylight saving time, leap seconds, etc., it seems safe to use the Time class. ..

2. Date class is good at date processing

It is convenient to use the Date class for processing by date or year.


[1]pry(main)> Date.today.next_month #Same day next month
=> #<Date: 2020-10-29 ((2459152j,0s,0n),+0s,2299161j)>

[2] pry(main)> Date.new(2020,10,-1) #Last day (October 2020)
=> #<Date: 2020-10-31 ((2459154j,0s,0n),+0s,2299161j)>

[3] pry(main)> Date.today.leap? #Judgment of leap year (this year = 2020)
=> true

[4] pry(main)> Date.today.next_year.leap? #Judgment of leap year (next year = 2021)
=> false

[5] pry(main)> Date.today + 3 #3 days later
=> #<Date: 2020-10-02 ((2459125j,0s,0n),+0s,2299161j)>

[6] pry(main)> Date.today - 7 #1 week ago (7 days ago)
=> #<Date: 2020-09-22 ((2459115j,0s,0n),+0s,2299161j)>

[7] pry(main)> Date.today >> 6 #Half a year later (6 months later)
=> #<Date: 2021-03-29 ((2459303j,0s,0n),+0s,2299161j)>

[8] pry(main)> Date.today << 1 #1 month ago
=> #<Date: 2020-08-29 ((2459091j,0s,0n),+0s,2299161j)>

It is convenient to be able to easily obtain the same day or the end of the next month, and determine the leap year. Also, date increment and decrement operations are a bit cumbersome in the Time class because they have to be handled in seconds. In that respect, in the Date class, daily units can be easily set to + and-, and monthly units can be set to << and >>.

3. 3. Conclusion

Basically, you can use the Time class when you want to display up to the time, but it is convenient to use the Date class when you mainly process by day, month, or year.

Recommended Posts

Handling of date and time in Ruby. Use Date and Time properly.
Explanation of Ruby Time and Date objects
Usability of date and time classes in each language
Use of Abstract Class and Interface properly in Java
Handling of line beginning and line ending in regular expressions in Ruby
Date and time
Summary of hashes and symbols in Ruby
[Ruby] Classification and usage of loops in Ruby
Use Shift Right and Shift Left properly in Selenium
Use of Date class
Use of Japanese fonts and external characters in JasperReport
Change date and time to Japanese notation in Rails
[Ruby] Exception handling in functions
Use ruby variables in javascript.
Judgment of fractions in Ruby
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Basics of sending Gmail in Ruby
Implementation of ls command in Ruby
[Java8] Proper use of Comparable and Comparator in terms of employee sorting
The design concept of Java's Date and Time API is interesting
Proper use of redirect_to and render
[Ruby] then keyword and case in
Handling of time zones using Java
Write keys and values in Ruby
[Ruby] Date, Time, Datetime class basics
[MySQL] [java] Receive date and time
Proper use of Mockito and PowerMock
Which class should I use to get the date and time in my Rails app (Time, DateTime, TimeWithZone)
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
This and that of the implementation of date judgment within the period in Java
[Ruby] I want to display posted items in order of newest date
I tried to make full use of the CPU core in Ruby
How to get date data in Ruby
Acquisition of article information in ruby ​​scraping
Make bubble sort and selection sort in Ruby
Use selenium (Firefox) in Ruby in WSL environment
Proper use of interface and abstract class
Tips for gRPC error handling in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Use selenium (Chrome) in Ruby in WSL environment
Comparison of JavaScript objects and Ruby classes
Postgresql gem doesn't install properly in Ruby 3.0
Discrimination of Enums in Java 7 and above
Use hashes well in Ruby to calculate the total amount of an order
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
Parse the date and time string formatted by the C asctime function in Java
Format the date and time given by created_at
[Java] Handling of JavaBeans in the method chain
[Ruby] Behavior of evaluation of conditional expression in while
[Ruby] Arguments with keywords and default values of arguments
I want to use arrow notation in Ruby
About date / time type of Doma2 entity class
[Ruby] How to use gsub method and sub method
Recommendation of Service class in Ruby on Rails
[Understanding] Differences between hashes and arrays in Ruby
Feel the passage of time even in Java
Put CSV files containing "'" and "" "in MySQL in Ruby 2.3
[Ruby] "Reference to object" and "Contents of variable"
Ruby on Rails ~ Basics of MVC and Router ~
Date and time acquisition method using DateAndTime API
Criteria for proper use of render and redirect_to