Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Organization

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

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] # => "月"
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?