I’m still not able to get this to work! The date and time are still
coming up like this “Tue Apr 04 00:00:00 GMT Daylight Time 2006”. The
following is what I have put in my controller:
def create @tblregisteredphone =
blregisteredphone.new(@params[‘tblregisteredphone’]) @tblregisteredphone.txtregisterdatetime = Time.now("%d-%m-%Y %I:%M%p")
if @tblregisteredphone.save
redirect_to :action => ‘list’
else
render_action ‘new’
end
end
Thank you! That is now causing both the date and time to display, but
rather oddly, the formatting is remaining as “Tue Apr 04 13:57:00 GMT
Daylight Time 2006” despite the code ("%d-%m-%Y %I:%M%p")…
I’m still not able to get this to work! The date and time are still
coming up like this “Tue Apr 04 00:00:00 GMT Daylight Time 2006”. The
following is what I have put in my controller:
If txtregisterdatetime is a string/varchar field, then setting it to
Time.now.strftime("%d-%m-%Y %I:%M%p") and saving it should store the
formatted version. But if it’s a time/date field, it’s always going to
get stored in the database’s internal format, and reformatting it before
saving won’t acccomplish anything.
Generally, you should store times and dates as the apporpriate time/date
datatype in the database, and not as text strings. You reformat whenever
you want to use it, into an instance variable like, say
I’m still not able to get this to work! The date and time are still
coming up like this “Tue Apr 04 00:00:00 GMT Daylight Time 2006”. The
following is what I have put in my controller:
def create @tblregisteredphone =
blregisteredphone.new(@params[‘tblregisteredphone’]) @tblregisteredphone.txtregisterdatetime = Time.now("%d-%m-%Y %I:%M%p")
if @tblregisteredphone.save
redirect_to :action => ‘list’
else
render_action ‘new’
end
end
Rails date and time helpers return Date classes,
which do not have the strftime method, how can
I convert a Date to a Time?
Check out the Rails Framework Documentation at http://api.rubyonrails.org/
It’ll help you a lot. For this situation, scroll the left, bottom pane
down
to the “to_time” methods. I’m pretty sure you’ll find your solution
there.
Best regards,
Bill
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.