LoginSignup
0
0

More than 5 years have passed since last update.

[Ruby] ActiveSupport Timeのサンプル

Last updated at Posted at 2018-10-12

自分の環境にactive_supportがあるか確認

$ gem which active_support

もし無い場合

$ gem install activesupport

か

$ vim Gemfile
gem 'activesupport'     // 追記
$ bundle install

とてもシンプルなサンプルコード

#!/usr/bin/ruby

require 'active_support/time'
# require 'active_support/all' でも可

### 現在日時取得(Timeオブジェクト)
now = Time.now
p now
# => 2018-10-12 17:23:43 +0900

### 分解
p now.strftime("%Y-%m-%d")
# => "2018-10-12"
p now.strftime("%H:%M:%S")
# => "17:23:43"
p now.strftime("%z")
# => "+0900"

### 加減算
p (now - 1.second).strftime("%Y-%m-%dT%H:%M:%S")
# => "2018-10-12T17:23:42"
p (now + 1.minute).strftime("%Y-%m-%dT%H:%M:%S")
# => "2018-10-12T17:24:43"
p (now - 1.hour).strftime("%Y-%m-%dT%H:%M:%S")
# => "2018-10-12T16:23:43"
p (now + 1.day).strftime("%Y-%m-%dT%H:%M:%S")
# => "2018-10-13T17:23:43"

おわり

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