Forum: Ruby on Rails Date/Time format and syntax

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.
Alana (Guest)
on 2006-04-04 15:05
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
David T. (Guest)
on 2006-04-04 15:19
(Received via mailing list)
On 04/04/06, Alana <removed_email_address@domain.invalid> wrote:
> to
>
>     2006-04-04 12:04:00
>
> ??

This should help you out:
http://ruby-doc.org/core/classes/Time.html#M000249

David
Gustav P. (Guest)
on 2006-04-04 15:20
> 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

Try...

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 P.
removed_email_address@domain.invalid
Alana (Guest)
on 2006-04-04 16:44
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
David T. (Guest)
on 2006-04-04 16:52
(Received via mailing list)
On 04/04/06, Alana <removed_email_address@domain.invalid> 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
Daniel Butler (Guest)
on 2006-04-04 16:58
(Received via mailing list)
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-da...

Daniel
Alana (Guest)
on 2006-04-04 16:59
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
Steve K. (Guest)
on 2006-04-04 17:16
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
Alana (Guest)
on 2006-04-04 17:19
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 :)
Kris (Guest)
on 2006-05-17 16:19
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.
Jonathan V. (Guest)
on 2006-05-17 16:26
(Received via mailing list)
Date.today.to_time

Time.now.to_date

-Jonathan.
Bill W. (Guest)
on 2006-05-17 17:28
(Received via mailing list)
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
This topic is locked and can not be replied to.