Ruby 3.0.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Timeクラス > strftime

instance method Time#strftime

strftime(format) -> String[permalink][rdoc]

時刻を format 文字列に従って文字列に変換した結果を返します。

[PARAM] format:
フォーマット文字列を指定します。使用できるものは 以下の通りです。

このメソッドは strftime(3) や glibcの仕様を参考に作成されており、以下のオプションが利用できます。

EとOは無視されます。



p t = Time.new(2001,2,3,4,5,6,"+09:00")  # => 2001-02-03 04:05:06 +0900
p t.strftime("Printed on %m/%d/%Y")      # => "Printed on 02/03/2001"
p t.strftime("Printed on %m/%-d/%_6Y")   # => "Printed on 02/3/  2001"
p t.strftime("at %I:%M%p")               # => "at 04:05AM"
p t.strftime("at %I:%M%#p")              # => "at 04:05am"
様々なISO 8601形式

t = Time.new(2001,2,3,4,5,6,"+09:00")
p t.strftime("%Y%m%d")           # => 20010203                  Calendar date (basic)
p t.strftime("%F")               # => 2001-02-03                Calendar date (extended)
p t.strftime("%Y-%m")            # => 2001-02                   Calendar date, reduced accuracy, specific month
p t.strftime("%Y")               # => 2001                      Calendar date, reduced accuracy, specific year
p t.strftime("%C")               # => 20                        Calendar date, reduced accuracy, specific century
p t.strftime("%Y%j")             # => 2001034                   Ordinal date (basic)
p t.strftime("%Y-%j")            # => 2001-034                  Ordinal date (extended)
p t.strftime("%GW%V%u")          # => 2001W056                  Week date (basic)
p t.strftime("%G-W%V-%u")        # => 2001-W05-6                Week date (extended)
p t.strftime("%GW%V")            # => 2001W05                   Week date, reduced accuracy, specific week (basic)
p t.strftime("%G-W%V")           # => 2001-W05                  Week date, reduced accuracy, specific week (extended)
p t.strftime("%H%M%S")           # => 040506                    Local time (basic)
p t.strftime("%T")               # => 04:05:06                  Local time (extended)
p t.strftime("%H%M")             # => 0405                      Local time, reduced accuracy, specific minute (basic)
p t.strftime("%H:%M")            # => 04:05                     Local time, reduced accuracy, specific minute (extended)
p t.strftime("%H")               # => 04                        Local time, reduced accuracy, specific hour
p t.strftime("%H%M%S,%L")        # => 040506,000                Local time with decimal fraction, comma as decimal sign (basic)
p t.strftime("%T,%L")            # => 04:05:06,000              Local time with decimal fraction, comma as decimal sign (extended)
p t.strftime("%H%M%S.%L")        # => 040506.000                Local time with decimal fraction, full stop as decimal sign (basic)
p t.strftime("%T.%L")            # => 04:05:06.000              Local time with decimal fraction, full stop as decimal sign (extended)
p t.strftime("%H%M%S%z")         # => 040506+0900               Local time and the difference from UTC (basic)
p t.strftime("%T%:z")            # => 04:05:06+09:00            Local time and the difference from UTC (extended)
p t.strftime("%Y%m%dT%H%M%S%z")  # => 20010203T040506+0900      Date and time of day for calendar date (basic)
p t.strftime("%FT%T%:z")         # => 2001-02-03T04:05:06+09:00 Date and time of day for calendar date (extended)
p t.strftime("%Y%jT%H%M%S%z")    # => 2001034T040506+0900       Date and time of day for ordinal date (basic)
p t.strftime("%Y-%jT%T%:z")      # => 2001-034T04:05:06+09:00   Date and time of day for ordinal date (extended)
p t.strftime("%GW%V%uT%H%M%S%z") # => 2001W056T040506+0900      Date and time of day for week date (basic)
p t.strftime("%G-W%V-%uT%T%:z")  # => 2001-W05-6T04:05:06+09:00 Date and time of day for week date (extended)
p t.strftime("%Y%m%dT%H%M")      # => 20010203T0405             Calendar date and local time (basic)
p t.strftime("%FT%R")            # => 2001-02-03T04:05          Calendar date and local time (extended)
p t.strftime("%Y%jT%H%MZ")       # => 2001034T0405Z             Ordinal date and UTC of day (basic)
p t.strftime("%Y-%jT%RZ")        # => 2001-034T04:05Z           Ordinal date and UTC of day (extended)
p t.strftime("%GW%V%uT%H%M%z")   # => 2001W056T0405+0900        Week date and local time and difference from UTC (basic)
p t.strftime("%G-W%V-%uT%R%:z")  # => 2001-W05-6T04:05+09:00    Week date and local time and difference from UTC (extended)