2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Ruby]日付系クラス(Time, Date, TimeDate)のメソッド

Last updated at Posted at 2018-01-26

1年くらい前にRubyの勉強のために日付系クラスの便利メソッドをまとめたもの

参考
パーフェクトRuby
https://docs.ruby-lang.org/ja/latest/class/Time.html

文字列表現

Time#strftime
Time オブジェクトから日時を表す文字列を任意のフォーマットで得ることができる

time = Time.now
time.strftime('%Y/%m/%d %H:%M:%S') # => "2017/05/20 21:43:10"

閏年判定

Date#leap?
閏年かどうか判定する

require 'date'

Date.new(2000).leap? # => true
Date.new(2001).leap? # => false

時刻から曜日を返す
Time#wday
曜日を数値で取得する。(0が日曜日、6が土曜日)

wdays = %w[日 月 火 水 木 金 土]
day = Time.now # => 2017-05-22 00:03:47 +0900
wdays[day.wday] # => "月"
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?