# Date formatting error

**URL:** <https://www.ruby-forum.com/t/date-formatting-error/70057>\
**Category:** Rails\
**Created:** [February 18, 2006, 5:58am UTC](https://www.ruby-forum.com/t/date-formatting-error/70057 "2006-02-18T05:58:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@sams](https://www.ruby-forum.com/u/sams)\
**Post date:** [February 18, 2006, 5:58am UTC](https://www.ruby-forum.com/t/date-formatting-error/70057/1 "2006-02-18T05:58:10Z")

</div>

Hi All,

I’m a bit stumped. All I’m trying to do is format a date and I get an  
error. Details below.

I have a partial to list all “notes” associated with a “client” with the  
following code.

\<% for note in @client.notes %\>

* * *
\<%=h note.note\_text %\> Posted on \<%=h note.created\_at.strftime("%I:%M") %\> by \<%=h note.created\_by %\> \<% end %\>

Which gives me the error…

|“You have a nil object when you didn’t expect it! The error occured  
while evaluating nil.strftime”

When I remove the |.strftime("%I:%M") from note.created\_at the display  
works correctly showing the data with the default format of “Fri Feb 17  
22:05:10 Central Standard Time 2006 by Sam S.”

I’m still fairly new to Rails so please bear with me.

Thanks,

Sam

---

<div class="post-metadata">

**Author:** ![sams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@sams](https://www.ruby-forum.com/u/sams)\
**Post date:** [February 18, 2006, 6:10am UTC](https://www.ruby-forum.com/t/date-formatting-error/70057/2 "2006-02-18T06:10:14Z")

</div>

On Fri, 2006-02-17 at 22:54 -0600, Sam S. wrote:

> \<%=h note.note\_text %\>  
> works correctly showing the data with the default format of “Fri Feb  
> 17 22:05:10 Central Standard Time 2006 by Sam S.”
> 
> I’m still fairly new to Rails so please bear with me.

* * *

is it possible that you have records in your table that don’t have this  
data? Did you create the records first, then add the ‘created\_at’ field  
later which might mean that you have records where this field is null?

Craig

---

<div class="post-metadata">

**Author:** ![sams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@sams](https://www.ruby-forum.com/u/sams)\
**Post date:** [February 18, 2006, 6:28am UTC](https://www.ruby-forum.com/t/date-formatting-error/70057/3 "2006-02-18T06:28:23Z")

</div>

Craig W. wrote:

> > while evaluating nil.strftime"  
> > data? Did you create the records first, then add the ‘created\_at’ field

I did add the field in later but it does have the correct data it shows  
it as below without the formatting.

“Fri Feb 17 22:05:10 Central Standard Time 2006 by Sam S.”

That is why I’m stumped at the error I get when attempting to format the  
data.

Sam

---

<div class="post-metadata">

**Author:** ![sams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@sams](https://www.ruby-forum.com/u/sams)\
**Post date:** [February 18, 2006, 6:40am UTC](https://www.ruby-forum.com/t/date-formatting-error/70057/4 "2006-02-18T06:40:29Z")

</div>

On Fri, 2006-02-17 at 22:54 -0600, Sam S. wrote:

> \<%=h note.note\_text %\>  
> works correctly showing the data with the default format of “Fri Feb  
> 17 22:05:10 Central Standard Time 2006 by Sam S.”
> 
> I’m still fairly new to Rails so please bear with me.

* * *

try removing the ‘h’

\<%= note.created\_at.strftime("%I:%M") %\>

Craig

---

<div class="post-metadata">

**Author:** ![sams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@sams](https://www.ruby-forum.com/u/sams)\
**Post date:** [February 18, 2006, 6:49am UTC](https://www.ruby-forum.com/t/date-formatting-error/70057/5 "2006-02-18T06:49:34Z")

</div>

Craig W. wrote:

> > while evaluating nil.strftime"

Nope, same error.

---

<div class="post-metadata">

**Author:** ![sams](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e79b87/32.png) [@sams](https://www.ruby-forum.com/u/sams)\
**Post date:** [February 18, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/date-formatting-error/70057/6 "2006-02-18T19:26:28Z")

</div>

Craig W. wrote:

> On Fri, 2006-02-17 at 22:54 -0600, Sam S. wrote:
> 
> > \<%=h note.note\_text %\>  
> > works correctly showing the data with the default format of “Fri Feb  
> > 17 22:05:10 Central Standard Time 2006 by Sam S.”
> > 
> > I’m still fairly new to Rails so please bear with me.
> 
> * * *
> 
> try removing the ‘h’
> 
> \<%= note.created\_at.strftime("%I:%M") %\>
> 
> Craig

I can’t seem to reproduce the error, using some test data the strftime  
you are using worked flawlessly. Perhaps try updating Rails to the  
latest version? If that doesn’t work I’m afraid I’m out of ideas.
