[RAILS] How to get date data in Ruby

Use class method for Date class

In Ruby, Date class is prepared as a class that handles dates, and by using this, it is easy to calculate data that is difficult to calculate accurately with just numbers such as leap year, number of days per month, management of days of the week, etc. It will be possible to obtain it. In addition to dates, there are classes that manage time and classes that handle both dates and times.

Main classes on dates and times

name of the class Manageable data
Date class date
Time class time
DateTime class Both date and time

Preparation for using Date class (require)

When using the Date class, you need to call the "Date "library on your Ruby code. The require method plays that role, which makes it possible to refer to an external file and get the class defined there. ~~ * In the case of Rails application, the class can be referenced in any file, so the description of require" ~ " is unnecessary ~~ (corrected below) ** * In the case of Rails application, it is not necessary to describe require "~" because it has a function to automatically read the reference file according to the specifications of the Rails framework **

・ Specific usage

require "date"
#Date library"date"To capture

The main way to get an instance (object) of the Date class

require "date"
today = Date.today
#Create a Date object by automatically calculating today's date

require "date"
today_str = "2020-12-30"
#December 30, 2020 Date object created

require "date"
today = Date.new(2020, 12, 30) 
#December 30, 2020 Date object created

Get year, month, etc. from the created Date object

To get specific information from the Date object created by the above method, use the following code.

require "date"
today = Date.today
today.year #Get the year from the created Date object
today.mon #Get the month from the created Date object
today.mday #Get the date from the created Date object
today.wday #Get the day of the week as a number from the created Date object

I would be grateful if you could point out any mistakes in the description. We are looking forward to hearing from you.

Recommended Posts

How to get date data in Ruby
How to get the date in java
How to get and add data from Firebase Firestore in Ruby
How to iterate infinitely in Ruby
How to get parameters in Spark
How to install Bootstrap in Ruby
How to implement date calculation in Java
[IOS] How to get data from DynamoDB
How to fix system date in JUnit
How to overwrite Firebase data in Swift
How to get keycloak credentials in interceptor class
How to get Class from Element in Java
[Ruby on Rails] How to install Bootstrap in Rails
How to build the simplest blockchain in Ruby
How to implement Pagination in GraphQL (for ruby)
I want to get the value in Ruby
How to get the current date as a string in yyyyMMdd format
[Ruby on Rails] How to write enum in Japanese
Rails / Ruby: How to get HTML text for Mail
How to use Ruby return
How to get information about associated tables in many-to-many tables
[Ruby] How to comment out
How to create a data URI (base64) in Java
[Ruby On Rails] How to reset DB in Heroku
How to launch another command in a Ruby program
How to write a date comparison search in Rails
How to store Rakuten API data in a table
Ruby: How to use cookies
How to handle TSV files and CSV files in Ruby
[Ruby] How to write blocks
[Ruby] How to get the tens place and the ones place
How to resolve SSL_connect error in PayPal Ruby SDK
How to get the class name / method name running in Java
How to use JSON data in WebSocket communication (Java, JavaScript)
How to change a string in an array to a number in Ruby
How to get values in real time with TextWatcher (Android)
How to retrieve the hash value in an array in Ruby
How to select a specified date by code in FSCalendar
How to display a graph in Ruby on Rails (LazyHighChart)
[IOS14] How to get Data type image data directly from PHPickerViewController?
[Ruby] How to batch convert strings in an array to numbers
How to use Lombok in Spring
How to find May'n in XPath
How to use Java HttpClient (Get)
How to hide scrollbars in WebView
How to get started with slim
How to run JUnit in Eclipse
Try to implement Yubaba in Ruby
How to use Spring Data JDBC
[Rails] How to write in Japanese
How to run Ant in Gradle
How to master programming in 3 months
How to install ruby through rbenv
[How to install Spring Data Jpa]
How to use Ruby on Rails
How to learn JAVA in 7 days
[Ruby] How to use any? Method
How to use InjectorHolder in OpenAM
Manipulating data in GCS during Ruby
How to introduce jQuery in Rails 6
How to use classes in Java?