Calculating a future date

Hello,

I'm new to Ruby so please bare with me...

I need to calculate a date that is exactly 31 days from the current
date in YYYY-MM-DD format. I know that Date.today returns the
current date, but how can I add 31 days to this value? I'm sure this
task is simple, but I haven't been able to figure it out.

Thanks

Time now + number of seconds in 31 days
Time.now + 31*24*60*60
which is equivalent to
  Time.now + 2678400

Farrel

···

On 06/02/07, Toine <bapolis@gmail.com> wrote:

Hello,

I'm new to Ruby so please bare with me...

I need to calculate a date that is exactly 31 days from the current
date in YYYY-MM-DD format. I know that Date.today returns the
current date, but how can I add 31 days to this value? I'm sure this
task is simple, but I haven't been able to figure it out.

Thanks

Hi,

This is something that took me ages to figure out because it is so simple :slight_smile:
Just add the number of days, eg future_date= Date.today+31

Cheers,
Dave

···

On 06/02/2007, at 9:40 PM, Farrel Lifson wrote:

On 06/02/07, Toine <bapolis@gmail.com> wrote:

Hello,

I'm new to Ruby so please bare with me...

I need to calculate a date that is exactly 31 days from the current
date in YYYY-MM-DD format. I know that Date.today returns the
current date, but how can I add 31 days to this value? I'm sure this
task is simple, but I haven't been able to figure it out.

Thanks

Time now + number of seconds in 31 days
Time.now + 31*24*60*60
which is equivalent to
Time.now + 2678400

Farrel

Date.today + 31 and Time.now + 31*24*60*60 yield the same results.

Thanks for your help.

···

On Feb 6, 2:47 am, Sharon Phillips <phillip...@yahoo.co.uk> wrote:

Hi,

This is something that took me ages to figure out because it is so
simple :slight_smile:
Just add the number of days, eg future_date= Date.today+31

Cheers,
Dave

On 06/02/2007, at 9:40 PM, Farrel Lifson wrote:

> On 06/02/07, Toine <bapo...@gmail.com> wrote:
>> Hello,

>> I'm new to Ruby so please bare with me...

>> I need to calculate a date that is exactly 31 days from the current
>> date in YYYY-MM-DD format. I know that Date.today returns the
>> current date, but how can I add 31 days to this value? I'm sure this
>> task is simple, but I haven't been able to figure it out.

>> Thanks

> Time now + number of seconds in 31 days
> Time.now + 31*24*60*60
> which is equivalent to
> Time.now + 2678400

> Farrel

Date.today + 31 and Time.now + 31*24*60*60 yield the same results.

Thanks for your help.

···

On Feb 6, 2:47 am, Sharon Phillips <phillip...@yahoo.co.uk> wrote:

Hi,

This is something that took me ages to figure out because it is so
simple :slight_smile:
Just add the number of days, eg future_date= Date.today+31

Cheers,
Dave

On 06/02/2007, at 9:40 PM, Farrel Lifson wrote:

> On 06/02/07, Toine <bapo...@gmail.com> wrote:
>> Hello,

>> I'm new to Ruby so please bare with me...

>> I need to calculate a date that is exactly 31 days from the current
>> date in YYYY-MM-DD format. I know that Date.today returns the
>> current date, but how can I add 31 days to this value? I'm sure this
>> task is simple, but I haven't been able to figure it out.

>> Thanks

> Time now + number of seconds in 31 days
> Time.now + 31*24*60*60
> which is equivalent to
> Time.now + 2678400

> Farrel

sender: "Toine" date: "Tue, Feb 06, 2007 at 08:00:06PM +0900" <<<EOQ

Date.today + 31 and Time.now + 31*24*60*60 yield the same results.

Just beware that although they yield the same result they do it at
*very* different speeds:

~>> cat date_vs_time.rb
require 'benchmark'
require 'date'

n = 100_000
Benchmark.bm do |x|
  x.report { n.times do a = Date.today+31; end }
  x.report { n.times do a = Time.now+2678400; end }
end

~>> ruby date_vs_time.rb
      user system total real
24.300000 0.760000 25.060000 ( 31.808122)
  0.410000 0.440000 0.850000 ( 0.971718)

So, whenever you can get away with Time, use it. Only use Date when
you have to.

Cheers,
Alex

require 'date'

puts Date.today
puts Date.today + 100
puts Time.now
puts Time.now + 100*24*60*60

2007-02-08
2007-05-19
Thu Feb 08 22:05:49 W. Europe Standard Time 2007
Sat May 19 23:05:49 W. Europe Daylight Time 2007

Looks innocent enough, but when I run the same program again in 60 minutes,
Time.now + 100*60*60 would output:
Sun May 20 00:05:49 W. Europe Daylight Time 2007 (which is my birthday by
the way).

So from a correctness point of view, use Date when you are dealing with
dates,
and use Time when you are dealing with time.

···

On 2/6/07, Alexandru E. Ungur <alexandru@globalterrasoft.ro> wrote:

>>> sender: "Toine" date: "Tue, Feb 06, 2007 at 08:00:06PM +0900" <<<EOQ
> Date.today + 31 and Time.now + 31*24*60*60 yield the same results.
Just beware that although they yield the same result they do it at
*very* different speeds:

~>> cat date_vs_time.rb
require 'benchmark'
require 'date'

n = 100_000
Benchmark.bm do |x|
  x.report { n.times do a = Date.today+31; end }
  x.report { n.times do a = Time.now+2678400; end }
end

~>> ruby date_vs_time.rb
      user system total real
24.300000 0.760000 25.060000 ( 31.808122)
  0.410000 0.440000 0.850000 ( 0.971718)

So, whenever you can get away with Time, use it. Only use Date when
you have to.

Cheers,
Alex

Assuming you want to (or need to) use Time, then you can... if you use ActiveSupport

#!/usr/bin/env ruby -w
require 'rubygems'
gem 'activesupport', '>=1.4'

require 'date'
puts "with Date:"
puts "today: #{Date.today}"
puts "+ 100: #{Date.today + 100}"
puts "with Time:"
puts " now: #{Time.now}"
puts "+ 8_640_000: #{Time.now + 100*24*60*60}"

puts "Time with some ActiveSupport from those Rails guys:"
require 'active_support'
puts " now: #{Time.now}"
puts " + 100.days: #{Time.now + 100.days}"
puts "in 100.days: #{Time.now.in(100.days)}"
__END__

with Date:
today: 2007-02-08
+ 100: 2007-05-19
with Time:
         now: Thu Feb 08 17:11:43 -0500 2007
+ 8_640_000: Sat May 19 18:11:43 -0400 2007
Time with some ActiveSupport from those Rails guys:
         now: Thu Feb 08 17:11:43 -0500 2007
  + 100.days: Sat May 19 18:11:43 -0400 2007
in 100.days: Sat May 19 17:11:43 -0400 2007

Notice that the last line has recognized (and corrected for) the Daylight Savings transition

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Feb 8, 2007, at 4:17 PM, Johan Veenstra wrote:

On 2/6/07, Alexandru E. Ungur <alexandru@globalterrasoft.ro> wrote:

>>> sender: "Toine" date: "Tue, Feb 06, 2007 at 08:00:06PM +0900" <<<EOQ
> Date.today + 31 and Time.now + 31*24*60*60 yield the same results.
Just beware that although they yield the same result they do it at
*very* different speeds:

~>> cat date_vs_time.rb
require 'benchmark'
require 'date'

n = 100_000
Benchmark.bm do |x|
  x.report { n.times do a = Date.today+31; end }
  x.report { n.times do a = Time.now+2678400; end }
end

~>> ruby date_vs_time.rb
      user system total real
24.300000 0.760000 25.060000 ( 31.808122)
  0.410000 0.440000 0.850000 ( 0.971718)

So, whenever you can get away with Time, use it. Only use Date when
you have to.

Cheers,
Alex

require 'date'

puts Date.today
puts Date.today + 100
puts Time.now
puts Time.now + 100*24*60*60

2007-02-08
2007-05-19
Thu Feb 08 22:05:49 W. Europe Standard Time 2007
Sat May 19 23:05:49 W. Europe Daylight Time 2007

Looks innocent enough, but when I run the same program again in 60 minutes,
Time.now + 100*60*60 would output:
Sun May 20 00:05:49 W. Europe Daylight Time 2007 (which is my birthday by
the way).

So from a correctness point of view, use Date when you are dealing with
dates,
and use Time when you are dealing with time.