LoginSignup
1
0

More than 1 year has passed since last update.

【Ruby on Rails】時間表示に関する記事いつも検索するからまとめてみた

Posted at

対象者

  • 時間の表示に関して調べている方

目的

  • 時間表示の方法を1つの記事にまとめて整頓する

実際の手順と実例

1.タイムゾーン

タイムゾーンを日本時間にする方法を下記の記事を参考にさせて頂きました。

config/application.rb
module App
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.time_zone = 'Tokyo'
  end
end

サーバー再起動で時間表示が日本時間にできます。

2.strftimeに関して

よく使うstrftimeは下記の記事をよく参考にさせて頂いております。

下記記事もまとまってて、使えそう

3.よく使うstrftimeテンプレート

「2021年08月30日 08:09:49」と表現するとき

time = Time.current
time.strftime("%Y年%m月%d日 %H:%M:%S")

「2021/08/30(月)」

wd = ["日", "月", "火", "水", "木", "金", "土"]
time = Time.current
time.strftime("%Y/%m/%d(#{wd[time.wday]})")

スクリーンショット 2021-08-30 8.17.38.png

wdayは曜日を0(日曜日)から6(土曜日)の整数で返してくれます。

Time#wday (Ruby 3.0.0 リファレンスマニュアル)

よく使う時間表示あったらまた更新します!

投稿者コメント

今日は自分用に時間に関する表示方法をまとめてみました。
これで毎度検索する手間をカットできそう

My Profile

プログラミング学習歴3ヶ月目のアカウントです!
プログラミングスクールで学んだ内容や自分が躓いた箇所等のアウトプットの為に発信しています。
また、プログラミング初学者の方にわかりやすく、簡潔にまとめて情報共有できればと考えています。
もし、投稿した記事の中に誤り等ございましたら、コメント欄でご教授いただけると幸いです。 

1
0
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
1
0