Posts Tagged ‘datetime’

Rails Helper to Remove Leading Zero in 12 Hour Time Format

Posted 01 Mar 2010 — by admin
Category Ruby on Rails

I can’t find a strftime() format that will output the hour without the leading zero. For instance 6:20 will be instead 06:20. This just looks a little sloppy. I created a datetime.rb intializer which will contain custom datetime formats for my application.

# RAILS_ROOT/config/initializers/datetime.rb
Time::DATE_FORMATS[:date] = "%a %b %d, %Y"
Time::DATE_FORMATS[:time] = "%I:%M%p"
# ...

Next I set up a helper function which will string together the :date and :time formats and remove the leading zero in the hour with the .gsub method on the string.

  # RAILS_ROOT/app/helpers/application_helper.rb
  def fulltime(created_at)
    created_at.to_s(:date)+" "+created_at.to_s(:time).gsub(/^0/,'').downcase
  end

In a view I’ll just pass the timestamp to the fulltime function

fulltime(@post.created_at)

Generate MySQL Datetime Type Using PHP Date() Function

Posted 05 Feb 2010 — by admin
Category php

If you want to insert a datetime that matches the default mysql datetime type format use this

date('Y-m-d H:i:s');

Date and Time Helpers in Rails

Posted 03 Oct 2009 — by admin
Category Ruby on Rails

Just for reference http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695

This post was created about <%= time_ago_in_words @post.created_at %> ago

Problem slash Bug in Rails with attr_accessor and Datetime Select Fields

Posted 07 Aug 2009 — by admin
Category Ruby on Rails

Looks like there is a problem with using the attr_accessor method with datetime form fields http://dev.rubyonrails.org/ticket/8983 In Rails 2.3.2, for me at least, the problem seems to be popping up again. While trying to process a date for credit card validation, I keep getting a ‘nil.klass’ error. Sure enough, remove the datetime select from the form and the problem goes away. I need to look into this a little further because I think that this has been resolved.

More information is available here

http://www.ruby-forum.com/topic/130229

and

http://www.google.com/search?q=1+error(s)+on+assignment+of+multiparameter+attributes&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

The temporary solution is to not use attr_accessor and rather add the field to the database with the correct data type

 t.datetime :your_datetime_attribute_you_were_once_virtualizing

. Everything then works as normal. Oh well…