Forum: Ruby on Rails Date verus Time class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kris (Guest)
on 2006-05-16 17:12
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.
Kris (Guest)
on 2006-05-17 16:21
This has to be simple, everybody must be doing it since the time and
date helpers return 'useless' Date classes...
D. Taylor S. (Guest)
on 2006-05-17 19:22
(Received via mailing list)
On 5/17/06, Kris <removed_email_address@domain.invalid> 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
Bill W. (Guest)
on 2006-05-17 20:25
(Received via mailing list)
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
Jeff Berg (Guest)
on 2006-05-17 20:50
(Received via mailing list)
This is a rails extension.

Try it in a "ruby script/console" setting.

Jeff
Bill W. (Guest)
on 2006-05-17 21:00
(Received via mailing list)
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
Ed Howland (Guest)
on 2006-05-17 21:45
(Received via mailing list)
On 5/17/06, Bill W. <removed_email_address@domain.invalid> 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
Bill W. (Guest)
on 2006-05-17 21:54
(Received via mailing list)
Ed Howland wrote:

> Actually script/console is a wrapper around irb.
<snip>

Thanks, Ed!  I appreciate the explanation.

Best regards,
Bill
Guest (Guest)
on 2006-05-24 16:16
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.
Rimantas L. (Guest)
on 2006-05-24 16:53
(Received via mailing list)
> 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
AFM (Guest)
on 2006-06-28 15:07
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
This topic is locked and can not be replied to.