# Checking date

**URL:** <https://www.ruby-forum.com/t/checking-date/94106>\
**Category:** Rails\
**Created:** [April 21, 2006, 12:29am UTC](https://www.ruby-forum.com/t/checking-date/94106 "2006-04-21T00:29:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [April 21, 2006, 12:29am UTC](https://www.ruby-forum.com/t/checking-date/94106/1 "2006-04-21T00:29:48Z")

</div>

Radrails created a field for date

```
  <%= product.date_available.strftime("%y-%m-%d") %>

```

What does this mean?  
Should the definition be date or datetime? I tried both and no results  
Jim

---

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [April 21, 2006, 12:55am UTC](https://www.ruby-forum.com/t/checking-date/94106/2 "2006-04-21T00:55:22Z")

</div>

SIX-S wrote:

> Radrails created a field for date  
> \<%= product.date\_available.strftime(“%y-%m-%d”) %\>  
> What does this mean?  
> That will convert the date “date\_available” into a string with the  
> format  
> yy-mm-dd as described here:  
> [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_time.html#Time.strftime)

> Should the definition be date or datetime? I tried both and no results

Not quite sure what you’re asking here.

Mark

---

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [April 21, 2006, 4:36am UTC](https://www.ruby-forum.com/t/checking-date/94106/3 "2006-04-21T04:36:37Z")

</div>

Thanks for answering my question.  
I dont know where the name " “strftime” originates  
I described the date\_available field in the table products(from tutorial  
“depot”) as type datetime. I changed the table type of date\_available as  
“date” when I had the error when running the program “list .rhtml” in my  
browser Firefox.  
JIm  
----- Original Message -----  
From: “Mark P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, April 20, 2006 4:55 PM  
Subject: [Rails] Re: checking date

---

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [April 21, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/checking-date/94106/4 "2006-04-21T13:04:05Z")

</div>

SIX-S wrote:

> Thanks for answering my question.  
> I dont know where the name " “strftime” originates  
> I described the date\_available field in the table products(from tutorial  
> “depot”) as type datetime. I changed the table type of date\_available as  
> “date” when I had the error when running the program “list .rhtml” in my  
> browser Firefox.  
> JIm

What error do you have that you are trying to correct?

datetime is the type stored in the database only, in your product object  
the attribute date\_available will be of class Time… strftime is a  
method of Time.

You may get an error when date\_available is not set in a record you wish  
to display/list, it will complain that no such method nil.strftime  
exists. Try the line:  
\<%= product.date\_available.strftime("%y-%m-%d") unless  
product.date\_available.nil? %\>

Or logic to that effect, alternatives can be found by searching the list  
archive.

Cheers,  
Mark

---

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [May 6, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/checking-date/94106/5 "2006-05-06T23:38:52Z")

</div>

I have a problem like the one described. My database has a a table  
paintings with a DATETIME field called creation\_date. In list.rhtml I  
code:

painting.creation\_date.strftime("%y-%m-%d")

Rails complains:  
You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.strftime

I’m sure the expression painting.creation\_date results in a variable of  
class Time, as the expression painting.creation\_date.class returns Time.

So why does Rails think that painting.creation\_date is a nil object?

---

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [May 7, 2006, 12:18am UTC](https://www.ruby-forum.com/t/checking-date/94106/6 "2006-05-07T00:18:37Z")

</div>

He, I found out why Rails complains about nil.strftime:  
I have rows in my database with no value for the field creation\_date.  
So, as a matter of fact, Rails doesn’t complain about errors in the  
code. While running the code Rails is checking (or reading) the database  
and finds nil values for the creation\_date fields of some rows. That’s  
what is reported as nil.strftime

---

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [May 7, 2006, 7:56am UTC](https://www.ruby-forum.com/t/checking-date/94106/7 "2006-05-07T07:56:21Z")

</div>

Anonymous wrote:

> He, I found out why Rails complains about nil.strftime:  
> I have rows in my database with no value for the field creation\_date.  
> So, as a matter of fact, Rails doesn’t complain about errors in the  
> code. While running the code Rails is checking (or reading) the database  
> and finds nil values for the creation\_date fields of some rows. That’s  
> what is reported as nil.strftime

For the record, Rails has a nifty little feature where if you create a  
table with a field named “created\_on” it will automatically populate new  
rows with the current date.

---

<div class="post-metadata">

**Author:** ![SIX-S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@SIX-S](https://www.ruby-forum.com/u/SIX-S)\
**Post date:** [May 7, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/checking-date/94106/8 "2006-05-07T23:04:20Z")

</div>

Thanks for the reply Adam. I knew about the feature and it is indeed  
useful. My creation\_date field though, wasn’t about the creation of the  
row but about the creation of the product itself. But I am certainly  
going to use created\_on.
