# Question about strftime when called from partial

**URL:** <https://www.ruby-forum.com/t/question-about-strftime-when-called-from-partial/60703>\
**Category:** Rails\
**Created:** [May 16, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/question-about-strftime-when-called-from-partial/60703 "2006-05-16T21:22:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 16, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/question-about-strftime-when-called-from-partial/60703/1 "2006-05-16T21:22:52Z")

</div>

Hi,

I’m calling a helper I’ve written - format\_time() that is called from my  
views with a given time that was selected from my database and was  
returned  
to the view via an instance variable. When I call format\_time from a  
partial, I see that I get some kind of String error and the partial  
won’t  
render. Basically, the object being passed in to format\_time is already  
a  
string and not in the Time format? Anything special going on with  
partials?

Thanks,.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 17, 2006, 1:48am UTC](https://www.ruby-forum.com/t/question-about-strftime-when-called-from-partial/60703/2 "2006-05-17T01:48:57Z")

</div>

Hello Sam,

2006/5/16, Sam D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’m calling a helper I’ve written - format\_time() that is called from my  
> views with a given time that was selected from my database and was returned  
> to the view via an instance variable. When I call format\_time from a  
> partial, I see that I get some kind of String error and the partial won’t  
> render. Basically, the object being passed in to format\_time is already a  
> string and not in the Time format? Anything special going on with  
> partials?

Can you be more specific ? Show us some code ? I do that all the  
time, and I have zero problems.

Bye !

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 17, 2006, 6:04am UTC](https://www.ruby-forum.com/t/question-about-strftime-when-called-from-partial/60703/3 "2006-05-17T06:04:54Z")

</div>

So here is the code:

module ApplicationHelper

def format\_time(time)  
time.strftime("%a %b %d, %I:%M%p")  
end  
end

and the partial gets the following error;

undefined method `strftime’ for “2006-05-13 15:15:00”:String

So basically it is looking at the object as a String instead of a Time  
object which is odd.

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 17, 2006, 6:20am UTC](https://www.ruby-forum.com/t/question-about-strftime-when-called-from-partial/60703/4 "2006-05-17T06:20:14Z")

</div>

I want to add one more thing in case somebody can figure this out, so I  
created another helper just for that partial called:

def format\_date(time)  
new\_time = Time.local(\*(ParseDate.parsedate(time)))  
new\_time.strftime("%a %b %d, %I:%M%p")  
end

And this worked. So then the question is, why is it that it is coming  
back  
as a String rather than a Time object. The above code just takes that  
string and converts it to a Time object and then does the same  
formatting ?

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 17, 2006, 8:04pm UTC](https://www.ruby-forum.com/t/question-about-strftime-when-called-from-partial/60703/5 "2006-05-17T20:04:10Z")

</div>

On May 16, 2006, at 9:03 PM, Sam D. wrote:

> undefined method `strftime’ for “2006-05-13 15:15:00”:String  
> So basically it is looking at the object as a String instead of a  
> Time object which is odd.

Replace that with:

class Time  
def format  
strtime “%a %b %d, %I:%M%p”  
end  
end

Then just call format on your Time instance directly.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
