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
3
Help us understand the problem. What are the problem?

posted at

updated at

Railsのstrftimeの使い方と実例

strftimeを使う時にいちいち調べるのが面倒なので、チートシートを作りました。
参考にしてみてください。

2018年03月01日

time = Time.now
time.strftime("%Y年%m月%d日")

=> "2018年03月01日"

2018/03/01

time = Time.now
time.strftime("%Y/%m/%d")

=> "2018/03/01"

18/03/01

time = Time.now
time.strftime("%y/%m/%d")

=> "18/03/01"

午前23:24 or 午後23:24

AM 09:24 or PM 23:24

*取得した時間が午前か午後かで変わります

time = Time.now
time.strftime("%p %R")

=> "AM 09:24" or "PM 23:24"

am 09:24 or pm 23:24

*取得した時間が午前か午後かで変わります

time = Time.now
time.strftime("%P %R")

=> "am 09:24" or "pm 23:24"

23時59分

time = Time.now
time.strftime("%H時%S分")

=> "23時57分"

午前11時59分 or 午後11時59分

time = Time.now

time.strftime("%H時%S分")

=> "23時57分"

March 01, 2018

time = Time.now
time.strftime("%B %d, %Y")

=> "March 01, 2018"

Mar 01, 2018

time = Time.now
time.strftime("%b %d, %Y")

=> "Mar 01, 2018"

参考にしたサイト

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
3
Help us understand the problem. What are the problem?