# Dates, scheduling tasks, mysql

**URL:** <https://www.ruby-forum.com/t/dates-scheduling-tasks-mysql/77821>\
**Category:** Ruby\
**Created:** [November 30, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/dates-scheduling-tasks-mysql/77821 "2006-11-30T21:01:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comforteagle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@comforteagle](https://www.ruby-forum.com/u/comforteagle)\
**Post date:** [November 30, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/dates-scheduling-tasks-mysql/77821/1 "2006-11-30T21:01:48Z")

</div>

What is the best Date format for working with dates in ruby & mysql?

I have a recurring task that once complete I want to update the db to  
reflect the next time to do it.

“2006-11-30” as a string ends up being just “2006”.

tnx in advance.

---

<div class="post-metadata">

**Author:** ![comforteagle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@comforteagle](https://www.ruby-forum.com/u/comforteagle)\
**Post date:** [November 30, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/dates-scheduling-tasks-mysql/77821/2 "2006-11-30T21:51:25Z")

</div>

Comfort E. wrote:

> What is the best Date format for working with dates in ruby & mysql?

As to MySQL, you really need to stick to a strict date format of  
yyyy-mm-dd.  
This is MySQL’s date format when shown in plain text.

> I have a recurring task that once complete I want to update the db to  
> reflect the next time to do it.
> 
> “2006-11-30” as a string ends up being just “2006”.

This didn’t happen by magic, you created it with some code. If you show  
us  
the code, we will show you the magic.

---

<div class="post-metadata">

**Author:** ![comforteagle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@comforteagle](https://www.ruby-forum.com/u/comforteagle)\
**Post date:** [December 5, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/dates-scheduling-tasks-mysql/77821/3 "2006-12-05T23:18:56Z")

</div>

Comfort E. wrote:

> > 
> 
> p date  
> dbh = Mysql.real\_connect(“localhost”, “xxx”, “xxxx”, “xxxxx”)  
> chg = dbh.query(“UPDATE sites set updated=’#{date}’ where  
> url=’#{url}’”)

Not sure why exending Date doesn’t work (at least in irb), but I believe  
you  
want to be defining the method ‘now’ (not Date.now??)

As an alternate way to do this you could try

$ irb  
irb(main):001:0\> date = Time.now.strftime(’%Y-%m-%d’)  
=\> “2006-12-05”  
irb(main):002:0\>

hth,  
Vance

---

<div class="post-metadata">

**Author:** ![comforteagle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@comforteagle](https://www.ruby-forum.com/u/comforteagle)\
**Post date:** [November 30, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/dates-scheduling-tasks-mysql/77821/4 "2006-11-30T22:22:52Z")

</div>

Paul L. wrote:

> Comfort E. wrote:
> 
> > What is the best Date format for working with dates in ruby & mysql?
> 
> As to MySQL, you really need to stick to a strict date format of  
> yyyy-mm-dd.  
> This is MySQL’s date format when shown in plain text.
> 
> > I have a recurring task that once complete I want to update the db to  
> > reflect the next time to do it.
> > 
> > “2006-11-30” as a string ends up being just “2006”.
> 
> This didn’t happen by magic, you created it with some code. If you show  
> us  
> the code, we will show you the magic.

class Date  
def Date.now  
return Date.jd(DateTime.now.jd)  
end  
end

url =‘[http://xxx.com](http://xxx.com)’  
begin  
date = Date.now.to\_s  
p date  
dbh = Mysql.real\_connect(“localhost”, “xxx”, “xxxx”, “xxxxx”)  
chg = dbh.query(“UPDATE sites set updated=‘#{date}’ where  
url=‘#{url}’”)

---

<div class="post-metadata">

**Author:** ![comforteagle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@comforteagle](https://www.ruby-forum.com/u/comforteagle)\
**Post date:** [December 6, 2006, 12:01am UTC](https://www.ruby-forum.com/t/dates-scheduling-tasks-mysql/77821/5 "2006-12-06T00:01:11Z")

</div>

Vance A Heron wrote:

> Comfort E. wrote:
> 
> > > 
> > 
> > p date  
> > dbh = Mysql.real\_connect(“localhost”, “xxx”, “xxxx”, “xxxxx”)  
> > chg = dbh.query(“UPDATE sites set updated=’#{date}’ where  
> > url=’#{url}’”)
> 
> Not sure why exending Date doesn’t work (at least in irb), but I believe  
> you  
> want to be defining the method ‘now’ (not Date.now??)
> 
> As an alternate way to do this you could try
> 
> $ irb  
> irb(main):001:0\> date = Time.now.strftime(’%Y-%m-%d’)  
> =\> “2006-12-05”  
> irb(main):002:0\>

Hmm

Sorry I meant to write back… the above code works when you setup your  
mysql tables properly for accepting dates.
