Explanation of Ruby Time and Date objects

What are Time and Date objects?

An object that represents a date or time.

How to handle Time object

Current time

For example, you can get the current time with the now method.

irb(main):020:0> Time.now
=> 2020-11-13 23:26:39.001668 +0900

Arbitrary time

You can create a Time object that represents any date and time with the new method.

irb(main):021:0> datetime = Time.new(2020, 1, 1, 12, 30
)
=> 2020-01-01 12:30:00 +0900

Specify display format (Time)

You can display the date and time in the specified format by using the strftime method. For example, to display in a format such as ** 2020-01-01 12:30 **, write as follows.

irb(main):021:0> datetime = Time.new(2020, 1, 1, 12, 30
)
=> 2020-01-01 12:30:00 +0900
irb(main):022:0> datetime.strftime('%Y-%m-%d %H:%M')
=> "2020-01-01 12:30"
irb(main):023:0> 

How to handle Date object

Unlike the Time object, use this when you do not need to handle the time.

Note that you should load the date library in advance before using it.

irb(main):023:0> require 'date'
=> true
irb(main):024:0> Date
=> Date

Get today's date

Use the today method.

irb(main):025:0> Date.today
=> #<Date: 2020-11-13 ((2459167j,0s,0n),+0s,2299161j)>

Set any date

Use new as well as Time.

irb(main):029:0> Date.new(2020, 1, 1)
=> #<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>

Specify display format (Date)

Like the Time object, you can specify the format to represent the date. Here, it is displayed in ** 2020/01/01 ** format from the Date object.

irb(main):030:0> Date.new(2020, 1, 1).strftime('%Y/%m/%
d')
=> "2020/01/01"

Recommended Posts

Explanation of Ruby Time and Date objects
Handling of date and time in Ruby. Use Date and Time properly.
Date and time
Comparison of JavaScript objects and Ruby classes
Usability of date and time classes in each language
[Ruby] Date, Time, Datetime class basics
[MySQL] [java] Receive date and time
Explanation about Array object of Ruby
The design concept of Java's Date and Time API is interesting
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Summary of hashes and symbols in Ruby
[Ruby] Classification and usage of loops in Ruby
Explanation of Ruby on rails for beginners ①
Introduction and usage explanation of Font Awesome
Format the date and time given by created_at
About date / time type of Doma2 entity class
[Ruby] "Reference to object" and "Contents of variable"
Basics of Ruby
Ruby on Rails ~ Basics of MVC and Router ~
Date and time acquisition method using DateAndTime API
Date () and Calendar ()
The date time of java8 has been updated
Summary of Japan time setting and display method
Ruby and Gem
How to write offline real time Implementation example by ruby and C99 of F04
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
Speed comparison at the time of generation at the time of date conversion
Change date and time to Japanese notation in Rails
A rough note about Ruby arrays and hash objects
[Rails] Precautions when comparing date and time with DateTime
[Technical memo] About the advantages and disadvantages of Ruby
[Ruby] Class nesting, inheritance, and the basics of self
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Ruby] Classes and instances
Symbols and Destructive Ruby
[Java] Date / time operations
[Ruby] Big Decimal and DECIMAL
Ruby Learning # 29 Classes & Objects
mutable and immutable objects
Source of cellular objects
definition of ruby method
Ruby classes and instances
Ruby inheritance and delegation
Ruby variables and methods
[For beginners] Explanation of classes, instances, and statics in Java
Handle Java 8 date and time API with Thymeleaf with Spring Boot
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Ruby] Questions and verification about the number of method arguments
Set the date and time from the character string with POI
The nth and n + 1st characters of a Ruby string
[Ruby] Creating code using the concept of classes and instances
[Ruby] Display today's day of the week using Date class
Sample code to parse date and time with Java SimpleDateFormat
[Ruby] About the difference between 2 dots and 3 dots of range object.
Impressions and memories of openssl, curl, ruby, homebrew, rbenv installation
[Rails] Search method when date and time have each column
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv