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!
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
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)
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?
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).
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.