Datetime
"12:00".to_datetime
#=> 2015-07-15T12:00:00+00:00
# no-date-time value will use "today" to complete the missing part
Time
Time.now.to_i
#=> 1434313634
Time.at(1434313634)
#=> 2015-06-15 04:27:14 +0800
Time.now.strftime "%Y-%m-%d %H:%M:%S %z"
#=> "2015-06-15 04:28:54 +0800"
Time.now.strftime "%Y-%m-%d %H:%M:%S"
#=> "2015-06-15 04:29:07"
Time.now.strftime "%Y%m%d%H%M%S"
#=> "20150615042918"
Time.now.strftime "%Y%m%d%H%M%S%z"
#=> "20150615043053+0800"
Today
Date.today
#=> Wed, 05 Aug 2015
# Today in system time
Time.zone.today
#=> Wed, 05 Aug 2015
# Today in time zone
Time.find_zone!("Taipei").today
#=> Fri, 07 Aug 2015
# Today in specific time zone
Date.current
#=> Wed, 05 Aug 2015
# Today in time zone or in system time
Date.current
: When Time.zone
or config.time_zone
are set, returns Time.zone.today
, otherwise returns Date.today
, before use Date.current
, make sure you've configured the expected time zone and which one is your desired "Today"
# no time zone setup
# which one is your expected dateimte?
DateTime.current
#=> Thu, 06 Aug 2015 16:01:04 +0000
DateTime.now
#=> Fri, 07 Aug 2015 00:02:26 +0800
Yesterday
1.day.ago
# => Tue, 26 Jan 2016 04:20:05 UTC +00:00
Date.yesterday
# => Tue, 26 Jan 2016
Time.zone.yesterday
=> Tue, 26 Jan 2016
Time zone setup
Note: If you're using Postgres and the data type of datetime is timestamp
rather than timestamptz
, you may take a look at this stackoverflow-discussion first before changing the time zone configuration.
Run rake -D time
for a list of tasks for finding time zone names.
rake time:zones:all
: display all time zone namesrake time:zones:all -- OFFSET=0
: filter time zone names with OFFSETrake time:zones:local
: display local time zone names
#/config/application.rb
module RestaurantCore
class Application < Rails::Application
...
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Taipei'
...
end
end
.to_datetime
: converts string to datetime object in UTC time unless invoke .in_time_zone
method
"13:00".to_datetime
#=> Fri, 07 Aug 2015 13:00:00 +0000
"13:00".to_datetime.in_time_zone
#=> Fri, 07 Aug 2015 21:00:00 CST +08:00
#=> this also converts time according to time_zone
Time.zone.parse("date-time-string").to_datetime
: parse date-time-string to datetime object in current timezone without time conversion.
ActiveSupport::TimeZone["UTC"].parse("date-time-string")
: parse date-time-string to datetime object in specific timezone without time conversion.
Time.zone.parse('12:00').to_datetime`
#=> Fri, 07 Aug 2015 12:00:00 +0800
Time to Integer
Time.now.to_i
#=> 1453970334
(Time.now.to_f * 1000).to_i
#=> 1454035995623
DateTime.strptime("1502951224.0695884",'%s')
=> Thu, 17 Aug 2017 06:27:04 +0000
Date, time and time_zone
Refer to this great article
Get a Date from date_select
Date.new(*params[:any_key].sort.map(&:last).map(&:to_i))
Calculate the number of months between two dates, ignored day
# d1 and d2 are Date objects
# http://stackoverflow.com/a/9428676/1581551
# (d2.year * 12 + d2.month) - (d1.year * 12 + d1.mont)
d1 = "2017/6/12".to_date
d2 = "2018/4/3".to_date
(d2.year * 12 + d2.month) - (d1.year * 12 + d1.month)
#=> 10
# use rails view helper
# http://stackoverflow.com/a/9573099/1581551
# http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words
ActionController::Base.helpers.distance_of_time_in_words("2017/1/1", "2017/3/1")
=> "about 2 months"
Caculate the hours between two time
t1 = Time.new(2017, 5, 26, 13)
t2 = Time.new(2017, 6, 7, 13)
(t2 - t1) / 1.hours
=> 288.0
Reference
- Time
- Time
- Date.current
- Rails: Get the time from a datetime
- DateTime.current
- Working with time zones in Ruby on Rails
- Convert string to datetime in specific timezone
- Rails: How to parse date-time string into a specific time zone
- Ignoring timezones altogether in Rails and PostgreSQL
- Dealing With Time Zones Using Rails and Postgres
- Is Time.zone.now.to_date equivalent to Date.today?
- How I can get yesterday's date?
- Convert Ruby Date to Integer
- How to get the current time as 13-digit integer in Ruby?
- strftime
- The Exhaustive Guide to Rails Time Zones
- Use rails date_select without an activerecord model
- How to get a Date from date_select or select_date in Rails?
- Find number of months between two Dates in Ruby on Rails