Ruby/サンプル/現在の日時を取得する方法
の編集
Top
/
Ruby
/
サンプル
/
現在の日時を取得する方法
[
トップ
] [
編集
|
差分
|
バックアップ
|
添付
|
リロード
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
-- 雛形とするページ --
(no template pages)
#navi(../) * Rubyで現在の日時を取得する方法 [#fedfb252] Rubyで現在の日時を取得するサンプルコードを以下に記します。 #contents #htmlinsertpcsp(ll-top.html,ll-sp.html) * Timeのnowメソッドを使用して現在日時を取得する [#x7371efc] 下記サンプルコードでは、to_sメソッドを使用し文字列に変換しています。~ 通常の値などを使用する場合は、to_sは不要です。~ 表示するために文字列変換(to_s)しているだけです。 - サンプルコード dt = Time.now puts "Time.now : " + dt.to_s puts "year : " + dt.year.to_s puts "month : " + dt.month.to_s puts "day : " + dt.day.to_s puts "hour : " + dt.hour.to_s puts "min : " + dt.min.to_s puts "sec : " + dt.sec.to_s puts "week : " + dt.wday.to_s - 実行結果 Time.now : 2014-02-26 13:27:56 +0900 year : 2014 month : 2 day : 26 hour : 13 min : 27 sec : 56 week : 3 以上、Time.nowを利用した現在の日時取得方法でした。 #htmlinsertpcsp(ll-btm.html,ll-sp.html)
タイムスタンプを変更しない
#navi(../) * Rubyで現在の日時を取得する方法 [#fedfb252] Rubyで現在の日時を取得するサンプルコードを以下に記します。 #contents #htmlinsertpcsp(ll-top.html,ll-sp.html) * Timeのnowメソッドを使用して現在日時を取得する [#x7371efc] 下記サンプルコードでは、to_sメソッドを使用し文字列に変換しています。~ 通常の値などを使用する場合は、to_sは不要です。~ 表示するために文字列変換(to_s)しているだけです。 - サンプルコード dt = Time.now puts "Time.now : " + dt.to_s puts "year : " + dt.year.to_s puts "month : " + dt.month.to_s puts "day : " + dt.day.to_s puts "hour : " + dt.hour.to_s puts "min : " + dt.min.to_s puts "sec : " + dt.sec.to_s puts "week : " + dt.wday.to_s - 実行結果 Time.now : 2014-02-26 13:27:56 +0900 year : 2014 month : 2 day : 26 hour : 13 min : 27 sec : 56 week : 3 以上、Time.nowを利用した現在の日時取得方法でした。 #htmlinsertpcsp(ll-btm.html,ll-sp.html)
テキスト整形のルールを表示する