Date verus Time class

I’m using the date_select and datetime_select helpers in my view, and
they return Date classes from the params hash.

But how do I work with Date classes, they don’t print human readable
dates or times, Time classes work well I can use strftime("%H:%M") to
print to the screen.

Is it possible to convert a Date to a Time, Ive been tinkering in irb
but have got nowhere slowly.

I also find it a bit odd that the Time class includes a Date… If there
was no Date class I could understand a Time class having a date. There
is also DateTime class!

Many thanks, K.

This has to be simple, everybody must be doing it since the time and
date helpers return ‘useless’ Date classes…

On 5/17/06, Kris [email protected] wrote:

This has to be simple, everybody must be doing it since the time and
date helpers return ‘useless’ Date classes…

Try this:

date = Date.today
time = date.to_time

And you have your time object.


D. Taylor S.
Reality Technician
“Probably the closest things to perfection are the huge absolutely
empty holes that astronomers have recently discovered in space. If
there’s nothing there, how can anything go wrong?” - Richard Brautigan

D. Taylor S. wrote:

Try this:

date = Date.today
time = date.to_time

And you have your time object.

Exactly what I thought. I just tried it in irb and, instead, I got
“NoMethodError: undefined method `to_time’ for #<Date:
4907745/2,0,2299161>”

I started the irb session with "require ‘date’ "

Any ideas why the to_time failed?

Best regards,
Bill

Hi Jeff,

Jeff Berg wrote:

This is a rails extension.

Try it in a “ruby script/console” setting.

That works. So irb is interactive Ruby and script\console is
interactive
Rails?

Thanks,
Bill

On 5/17/06, Bill W. [email protected] wrote:

That works. So irb is interactive Ruby and script\console is interactive
Rails?

Actually script/console is a wrapper around irb. It loads it with the
current configuration environment (e.g. ‘development’,) the completion
library and takes an option to load a sandbox (-s, --sandbox) to roll
back any changes to your database you made with the console. You can
even swap out another irb for the one it defaults to
(–irb=/path/to/irb)

Ed

This is a rails extension.

Try it in a “ruby script/console” setting.

Jeff

Ed Howland wrote:

Actually script/console is a wrapper around irb.

Thanks, Ed! I appreciate the explanation.

Best regards,
Bill

I seem to have a problem with this:

d = Date.new
t = d.to_time

ArgumentError: argument out of range
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/core_ext/date/conversions.rb:26:in
‘local’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/core_ext/date/conversions.rb:26:in
‘to_time’
from (irb):20

I’m doing this from the console…

I also tried t = d.to_time(:local) which gives the same error…

Any ideas?

As a second thing how do I get the day as in Monday, tuesday etc. from a
date?

Many thanks, K.

I seem to have a problem with this:

d = Date.new
t = d.to_time
<…>
I also tried t = d.to_time(:local) which gives the same error…

Any ideas?

As a second thing how do I get the day as in Monday, tuesday etc. from a
date?
<…>

What are you trying to do? To get current (according to your system)
weekday?

wd = Date::DAYNAMES[Time.new.wday]

Time.new creates an instance of Time with the current system time.
Date.new creates
an instance of Date for Juliand Day zero, which is… far far away in
the past (and indeed out of range for Time wich counts from Jan 1st
1970).

Regards,
Rimantas

Rimantas L. wrote:

I seem to have a problem with this:

d = Date.new
t = d.to_time
<…>
I also tried t = d.to_time(:local) which gives the same error…

Any ideas?

As a second thing how do I get the day as in Monday, tuesday etc. from a
date?
<…>

What are you trying to do? To get current (according to your system)
weekday?

wd = Date::DAYNAMES[Time.new.wday]

This works, I did’nt know you had to put the class/module name before
the array you are trying to use. What does the :: mean?

Time.new creates an instance of Time with the current system time.
Date.new creates
an instance of Date for Juliand Day zero, which is… far far away in
the past (and indeed out of range for Time wich counts from Jan 1st
1970).

Regards,
Rimantas