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.
167a3b5582230623eb94e638133122ec?d=identicon&s=25 Kris (Guest)
on 2006-05-16 15: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.
167a3b5582230623eb94e638133122ec?d=identicon&s=25 Kris (Guest)
on 2006-05-17 14:21
This has to be simple, everybody must be doing it since the time and
date helpers return 'useless' Date classes...
09ff21c336ae126e8b3199f8b64d7796?d=identicon&s=25 D. Taylor Singletary (Guest)
on 2006-05-17 17:22
(Received via mailing list)
On 5/17/06, Kris <kris@alternativefocusmedia.com> 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 Singletary
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-05-17 18:25
(Received via mailing list)
D. Taylor Singletary 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
27cafc47e17bb64ce72586ec9bf52f5a?d=identicon&s=25 Jeff Berg (Guest)
on 2006-05-17 18:50
(Received via mailing list)
This is a rails extension.

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

Jeff
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-05-17 19: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
822a498b26a2cb7d1f0f2e7e37ce61b2?d=identicon&s=25 Ed Howland (Guest)
on 2006-05-17 19:45
(Received via mailing list)
On 5/17/06, Bill Walton <bill.walton@charter.net> 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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-05-17 19: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
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Guest (Guest)
on 2006-05-24 14: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.
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-05-24 14: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
167a3b5582230623eb94e638133122ec?d=identicon&s=25 AFM (Guest)
on 2006-06-28 13:07
Rimantas Liubertas 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.