Forum: Ruby on Rails Formatting timestamp objects

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.
6661ef9d747db3af8896cd94959d717d?d=identicon&s=25 Paul Barry (Guest)
on 2006-01-09 04:10
(Received via mailing list)
I want to print my timestamp objects in a specific format.  I want to
print
a date like this:

Sunday, January 8 2006

I don't want it to print January 08.

For the time, I want it to look like this:

9:08 pm

Not 09:08 and lower case PM.  I created these methods:

  def format_date(date)
    date.strftime("%A, %B #{date.day} #{date.year}")
  end

  def format_time(date)
    if date.hour > 12
      hour = date.hour - 12
    elsif date.hour == 0
      hour = 12
    else
      hour = date.hour
    end
    time = "#{hour}:#{date.min} "
    if date.hour > 12
      time += "pm"
    else
      time += "am"
    end
  end

There has to be a better way to do this.  Any ideas?

BTW - How do I know what the datatype of the properties that are created
by
AcitveRecord::Base are?
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-09 05:40
Paul Barry wrote:
> I want to print my timestamp objects in a specific format.  I want to
> print
> a date like this:
>
> Sunday, January 8 2006
>
> I don't want it to print January 08.
>
> For the time, I want it to look like this:
>
> 9:08 pm
>
> Not 09:08 and lower case PM.  I created these methods:
>

Try this for the time format

def format_time(date)
  date.strftime("%I:%M %p") =~ /(0*)([\d:]+) (\w+)/
  "#{$2} #{$3.downcase}"
end
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-09 06:26
(Received via mailing list)
Try this; it makes your view code very clean and dry.
http://rails.techno-weenie.net/tip/2005/11/20/defi...
17acfbd3a05cfb59e0543235cc0a813b?d=identicon&s=25 Joshua Susser (Guest)
on 2006-01-09 06:28
Paul Barry wrote:
> I want to print my timestamp objects in a specific format.  I want to
> print
> a date like this:
>
> Sunday, January 8 2006
>
> I don't want it to print January 08.
>
> For the time, I want it to look like this:
>
> 9:08 pm
>
> Not 09:08 and lower case PM.  I created these methods:
>
>   def format_date(date)
>     date.strftime("%A, %B #{date.day} #{date.year}")
>   end
>
> There has to be a better way to do this.  Any ideas?

Check out ActiveSupport Time and Date conversion modules. They extend
to_s to take a symbol specifying the format to use, eg
"Time.now.to_s(:db)" converts the time to the canonical database
representation of a datetime. Then you can put something like the
following in your environment.rb file:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
  :date_time12 => "%m/%d/%Y %I:%M%p",
  :date_time24 => "%m/%d/%Y %H:%M"
)

The use a regexp and .downcase to finish the job.

> BTW - How do I know what the datatype of the properties that are created
> by
> AcitveRecord::Base are?

AR::columns returns an array of columns.
Try printing
MyModel.columns.collect {|c| [c.name, c.type]}
6661ef9d747db3af8896cd94959d717d?d=identicon&s=25 Paul Barry (Guest)
on 2006-01-09 21:54
(Received via mailing list)
Use a regexp to find 08 and replace it with 8?  There's not a cleaner
way to
do that?
6661ef9d747db3af8896cd94959d717d?d=identicon&s=25 Paul Barry (Guest)
on 2006-01-09 22:12
(Received via mailing list)
Also, when I print Time.zone, I get "Eastern Standard Time".  How can I
get
"EST" instead?
This topic is locked and can not be replied to.