Which class should I use to get the date and time in my Rails app (Time, DateTime, TimeWithZone)

Which class should be used to get the date and time

--Time class --DateTime class --TimeWithZone class

In Rails, unless you have a specific reason It is better to unify with the TimeWithZone class rather than the Time class and DateTime class.

About time zone

There are three places to set the time zone.

--System --Environment variable ʻENV ['TZ'] `

Use Time.zone.name to see which timezone is set in application.rb.

# application.rb
config.time_zone = 'Tokyo'

Time.zone.name
=> "Tokyo"

Which time zone will be used

To complicate matters, the time zone used depends on the class and the methods associated with it. For example Time.now using the method now related to the Time class uses the timezone of the environment variable Time.current using the method current associated with the Time class uses the timezone of application.rb.

#Environment variables are used and class becomes Time
[1] pry(main)> Time.now
=> 2020-07-31 19:39:18 +0900
[2] pry(main)> Time.now.class
=> Time

#application.rb is used and class becomes TimeWithZone
[3] pry(main)> Time.current
=> Fri, 31 Jul 2020 19:39:35 JST +09:00
[4] pry(main)> Time.current.class
=> ActiveSupport::TimeWithZone

Use Time.current or Time.zone.now to get the current date and time

There are the following methods to get the current date and time.

  1. Time.now (use the timezone of the environment variable)
  2. DateTime.now (use the timezone of the environment variable)
  3. Time.current (use the timezone of application.rb)
  4. Time.zone.now (use the timezone in application.rb)

In conclusion, it's better to use the TimeWithZone class 3 or 4 (because if you want to make it an international application, you can handle it in various ways).

#The timezone of the environment variable is used
[1] pry(main)> Time.now
=> 2020-07-31 19:43:14 +0900 #Time class
[2] pry(main)> DateTime.now
=> Fri, 31 Jul 2020 19:43:21 +0900 #DateTime class

# application.rb timezone is used
[3] pry(main)> Time.current
=> Fri, 31 Jul 2020 19:43:26 JST +09:00 #ActiveSupport::TimeWithZone class
[4] pry(main)> Time.zone.now
=> Fri, 31 Jul 2020 19:43:32 JST +09:00 #ActiveSupport::TimeWithZone class

Reference article

I referred to the following article. The article explains it in great detail. https://qiita.com/jnchito/items/cae89ee43c30f5d6fa2c

Recommended Posts

Which class should I use to get the date and time in my Rails app (Time, DateTime, TimeWithZone)
Change date and time to Japanese notation in Rails
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Get the current date and time by specifying the time zone in Thymeleaf
How to get the date in java
Use Timecop in Rails web app to travel time on your browser
[Java] How to get the current date and time and specify the display format
[Rails] Use devise to get information about the currently logged in user
[Java Spring MVC] I want to use DI in my own class
I tried to organize the session in Rails
Unify the Rails app time zone to Japan time
I want to recursively get the superclass and interface of a certain class
I want to get the value in Ruby
[Java] How to use Calendar class and Date class
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
How to set the display time to Japan time in Rails
I want to use a little icon in Rails
[Rails] I don't know how to use the model ...
What should I use for the testing framework [Rails]
[Rails] Precautions when comparing date and time with DateTime
[Swift] Use UserDefaults to save data in the app
Handling of date and time in Ruby. Use Date and Time properly.
[Rails] Validate the start time (datetime type) and end time
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
How to get the class name / method name running in Java
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
What is the LocalDateTime class? [Java beginner] -Date and time class-
I want to use the Java 8 DateTime API slowly (now)
[Rails] Get access_token at the time of Twitter authentication with Sorcery and save it in DB
I tried to express the result of before and after of Date class with a number line
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
[Java] Get date information 10 days later using milliseconds in the Date class
[Rails] How to get the URL of the transition source and redirect
I tried to sort the data in descending order, ascending order / Rails
Things to remember and concepts in the Ruby on Rails tutorial
[Android] I want to get the listener from the button in ListView
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
How to use the wrapper class
[Ruby] Date, Time, Datetime class basics
I summarized the points to note when using resources and resources in combination
I stumbled upon the development and publication of my first iPhone app
How to get the current date as a string in yyyyMMdd format
(For beginners) [Rails] Time saving tech! How to install and use slim
[Rails] How to get the user information currently logged in with devise
I want to get the IP address when connecting to Wi-Fi in Java
I tried to make full use of the CPU core in Ruby
I translated the grammar of R and Java [Updated from time to time]
[Rails] How to apply the CSS used in the main app with Administrate
Is it possible to put the library (aar) in the Android library (aar) and use it?
How to create your own annotation in Java and get the value
Change the save destination of the image to S3 in the Rails app. Part 2
How to embed and display youtube videos in Rails (You can also get the URL you entered by editing)