Datetime question

Hi there. I’m just learning so bear with me. I have a column in my
database for date_created and it shows in the view as Wed Nov 28
18:05:00 -0600 2007. I don’t really want it to display like that so is
there a way to change how that date is formated in the view but not
changed in the database? The code I have looks like this:

<%= e.date_created %>

Also in the _form.rhtml for editing entries, scaffolding builds this:

<%= datetime_select ‘entry’, ‘date_created’ %>

Is there anyway to have that not show but still put the time created
in the database? If I remove the

Sorry, totally nuby question here I’m sure. I may be hounding you guys
again in the near future. Better to ask questions early and often.
Thanks so much in advance to anyone who can shed a little light on
this.

On 30 Nov 2007, at 12:48, wsakundi wrote:

Hi there. I’m just learning so bear with me. I have a column in my
database for date_created and it shows in the view as Wed Nov 28
18:05:00 -0600 2007. I don’t really want it to display like that so is
there a way to change how that date is formated in the view but not
changed in the database? The code I have looks like this:

<%= e.date_created %>

Sure, use strftime.

Fred

This should be more useful to you: http://railscasts.com/episodes/31

Thanks so much. I should have checked there first. I haven’t gotten
around to watching any of those yet which, I guess, I really should.
Those and my bloody PeepCode downloads. Anyway, thanks again for
pointing me in the right direction. I’d heard there was a supportive
community for Rails and there’s the proof!

wsakundi wrote:

Hi there. I’m just learning so bear with me. I have a column in my
database for date_created and it shows in the view as Wed Nov 28
18:05:00 -0600 2007. I don’t really want it to display like that so is
there a way to change how that date is formated in the view but not
changed in the database? The code I have looks like this:

<%= e.date_created %>

Basically, you can use strftime to change it to look like anything that
suits you. Look it up in the Ruby API.
it will be something like e.date_created.strftime("%d-%m-%Y") will give
you something like 02-12-2007.

Also in the _form.rhtml for editing entries, scaffolding builds this:

<%= datetime_select ‘entry’, ‘date_created’ %>

Is there anyway to have that not show but still put the time created
in the database? If I remove the

Scaffold automatically creates the field for created_on in the form.
You can delete it from the form. The user doesn’t need to enter a date
for it. Rails will automatically insert the created_on datetime when
the record is saved.

Sorry, totally nuby question here I’m sure. I may be hounding you guys
again in the near future. Better to ask questions early and often.
Thanks so much in advance to anyone who can shed a little light on
this

Welcome aboard!

Cheers,
Mohit.
12/2/2007 | 1:25 AM.

On Dec 1, 2007, at 2:45 PM, wsakundi wrote:

Thanks. I’ve tried removing datetime_select but it won’t put the time
into the table and then breaks because it’s returning a value of nil
when I want that date to display in my list of entries. I thought that
I could just put Time.now in there but, nope. That shows the time but
doesn’t put it into the database. I guess I have more reading to do.
Is that something I would fix in the Controller? or should I be
correcting that (somehow) in my view?

Maybe I’m missing something in this question, but wouldn’t:

<%= e.date_created.to_formatted_s(:short) %>

do the trick?

Put this in your config/environment.rb file at the bottom:

ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS.update(:default
=> ‘%m-%d-%Y’)
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default
=> ‘%m-%d-%Y’)

This will format ALL displayed time and date formats on your site to
appear as 12-24-2007 format, so you only have to do

e.date_created

Thanks. I’ve tried removing datetime_select but it won’t put the time
into the table and then breaks because it’s returning a value of nil
when I want that date to display in my list of entries. I thought that
I could just put Time.now in there but, nope. That shows the time but
doesn’t put it into the database. I guess I have more reading to do.
Is that something I would fix in the Controller? or should I be
correcting that (somehow) in my view?

Harmon wrote:

Put this in your config/environment.rb file at the bottom:

ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS.update(:default
=> ‘%m-%d-%Y’)
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default
=> ‘%m-%d-%Y’)

This will format ALL displayed time and date formats on your site to
appear as 12-24-2007 format, so you only have to do

e.date_created

Not sure if this bug still exists, but doing this used to hose MySQL
date columns. It would attempt to save the dates in the new format and
mysql might not recognize it and instead populate the field with null.

I don’t recommend altering the default format for this reason.

On Dec 12, 2007, at 11:12 PM, Kevin O. wrote:

This will format ALL displayed time and date formats on your site to
I don’t recommend altering the default format for this reason.
ActiveRecord should be using the :db format rather than the :default
when making date strings to be consumed by the database.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

If your column name is date_created, rails won’t automatically
populate it with the time. If you want rails to create it for you,
you should rename it to created_at, or created_on. If you don’t want
to rename it you can modify your create action in the controller with
something like this:
def create
@entry = Entry.new(params[:entry])
@entry.date_created = Time.now
if @entry.save…bla bla bla
…bla bla
end

There may be a better way, like putting it in your model. Maybe
someone else can chime in.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs