Hi, Just wondering how I get both the current date and time to display here (so far I can only get the date OR the time): @tblregisteredphone.txtregisterdatetime = Date.today Also, how can I change the format of the date and time from: Tue Apr 04 00:00:00 GMT Daylight Time 2006 to 2006-04-04 12:04:00 ?? I''m sure it''s very simple, I just don''t know how to do it! Alana -- Posted via http://www.ruby-forum.com/.
On 04/04/06, Alana <alana@c4l.co.uk> wrote:> Hi, > > Just wondering how I get both the current date and time to display here > (so far I can only get the date OR the time): > > @tblregisteredphone.txtregisterdatetime = Date.today > > Also, how can I change the format of the date and time from: > > Tue Apr 04 00:00:00 GMT Daylight Time 2006 > to > > 2006-04-04 12:04:00 > > ??This should help you out: http://ruby-doc.org/core/classes/Time.html#M000249 David
> Also, how can I change the format of the date and time from: > > Tue Apr 04 00:00:00 GMT Daylight Time 2006 > to > > 2006-04-04 12:04:00Try... datetime_for_formatting = Time.now # or other time object formatted_string = datetime_one.strftime("%Y-%m-%d %H:%M:%S") Checkout the Pickaxe for the Time class. strftime is really cool and can do a whole lot more! Hope this helps! Ciao! Gustav Paul gustav@psychohistorian.org -- Posted via http://www.ruby-forum.com/.
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 Can anyone tell me where I''m going wrong? Thank you for your replies, Alana -- Posted via http://www.ruby-forum.com/.
On 04/04/06, Alana <alana@c4l.co.uk> wrote:> 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")I think it should be Time.now.strftime("%d-%m-%Y %I:%M%p"), although I''ve not tried that to double check. David
If you want the ISO 8601 date and time format set globally for your Rails application, add the following to your environment.rb file: ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => ''%Y-%m-%d %H:%M:%S'') ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.update(:default => ''%Y-%m-%d'') Once you do that, you won''t have to format the dates and times in your controllers or views unless you want something other than the ISO format. From: http://jdb.org/articles/2006/02/10/iso-time-and-dates-in-ruby-on-rails Daniel On 4/4/06, Alana <alana@c4l.co.uk> wrote:> > 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 > > Can anyone tell me where I''m going wrong? > > Thank you for your replies, > > Alana > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/1ce43306/attachment.html
Hi David, 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")... But thats superficial anyway! Thank you, Alana -- Posted via http://www.ruby-forum.com/.
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 @formatted_time = @myobject.datefield.strftime("%d-%m-%Y %I:%M%p") and then display @formatted_time in your view. Alana wrote:> 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 > > Can anyone tell me where I''m going wrong? > > Thank you for your replies, > > Alana-- Posted via http://www.ruby-forum.com/.
Thank you all for your help, its all sorted now! I put the formatting in my environment.rb file and its all working quite happily! Alana :) -- Posted via http://www.ruby-forum.com/.
Rails date and time helpers return Date classes, which do not have the strftime method, how can I convert a Date to a Time? Many thanks. -- Posted via http://www.ruby-forum.com/.
Date.today.to_time Time.now.to_date -Jonathan. On 5/18/06, Kris <kris@alternativefocusmedia.com> wrote:> Rails date and time helpers return Date classes, which do not have the > strftime method, how can I convert a Date to a Time? > > Many thanks. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi Kris, Kris wrote:> 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