Skip to content

Instantly share code, notes, and snippets.

@photomattmills
Created May 2, 2018 01:49
Show Gist options
  • Save photomattmills/c7c9c03f45d957fbcc12145e60143f84 to your computer and use it in GitHub Desktop.
Save photomattmills/c7c9c03f45d957fbcc12145e60143f84 to your computer and use it in GitHub Desktop.

Dates in Ruby

So, you want to calculate some arbitrary date things? Easy! Let's use IRB! Open up the terminal (don't worry, there aren't any dragons here unless you've brought them). Then type irb and hit enter. it should bring up another prompt that looks something like this:

 > irb
irb(main):001:0>

What this is is a REPL, which stands for Read-Evaluate-Print Loop. It reads what you type when you press enter, evaluates, and prints the result. If you want more information on REPLs, the railsbridge docs are a pretty good primer.

With our REPL open, type require "time", which loads the ruby Time class. Let's say we have a date we want to turn into a Time object to do stuff with. That would look like:

irb(main):001:0> require 'time'
=> true
irb(main):002:0> date = Time.parse("January 1, 3012")
=> 3012-01-01 00:00:00 -0800

Now we have a date object, and there are a lot of methods on date to try; you can see them by calling date.methods:

irb(main):010:0> date.methods
=> [:+, :-, :<=>, :eql?, :min, :inspect, :succ, :to_a, :to_s, :to_i, :to_f, :hash, :round, :ctime, :zone, :utc, :to_r, :localtime, :gmtime, :getlocal, :getgm, :getutc, :asctime, :sec, :hour, :mday, :day, :mon, :month, :year, :wday, :yday, :isdst, :dst?, :gmtoff, :gmt_offset, :utc_offset, :utc?, :gmt?, :sunday?, :monday?, :tuesday?, :wednesday?, :thursday?, :friday?, :saturday?, :tv_sec, :tv_usec, :usec, :tv_nsec, :nsec, :subsec, :strftime, :rfc2822, :rfc822, :httpdate, :xmlschema, :iso8601, :to_time, :to_date, :to_datetime, :<, :>, :<=, :>=, :==, :between?, :clamp, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :singleton_method, :instance_variable_defined?, :define_singleton_method, :method, :public_method, :instance_variable_set, :extend, :to_enum, :enum_for, :===, :=~, :!~, :respond_to?, :freeze, :object_id, :send, :display, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]

The second one is what we need. - will let us subtract either two times from each other, or an integer number of seconds (You can also add two times, or add seconds to a time). So, to figure out our solution:

irb(main):008:0> date - 362_664*24*3600
=> 2019-01-22 00:00:00 -0800

We take the number of days we want to subtract, multiply that times 24 for hours, and times 3600 for seconds. Ruby will respect order of operations automatically when doing arithmetic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment