# Getting current date

**URL:** <https://rubytalk.org/t/getting-current-date/49021>\
**Category:** ruby-talk\
**Created:** [12 September 2008 03:33 UTC](https://rubytalk.org/t/getting-current-date/49021 "2008-09-12T03:33:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado1](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@laredotornado1](https://rubytalk.org/u/laredotornado1)\
**Post date:** [12 September 2008 03:33 UTC](https://rubytalk.org/t/getting-current-date/49021/1 "2008-09-12T03:33:26Z")

</div>

Hi,

How do I get the current date in the form "YYYY-MM-DD".

Thanks, - Davegro

---

<div class="post-metadata">

**Author:** ![Dan6](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Dan6](https://rubytalk.org/u/Dan6)\
**Post date:** [12 September 2008 03:38 UTC](https://rubytalk.org/t/getting-current-date/49021/2 "2008-09-12T03:38:29Z")

</div>

irb(main):005:0\> Date.today.to\_s  
=\> "2008-09-11"

See [http://www.ruby-doc.org/core/classes/Date.html](http://www.ruby-doc.org/core/classes/Date.html)

---

<div class="post-metadata">

**Author:** ![laredotornado1](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@laredotornado1](https://rubytalk.org/u/laredotornado1)\
**Post date:** [12 September 2008 17:13 UTC](https://rubytalk.org/t/getting-current-date/49021/3 "2008-09-12T17:13:12Z")

</div>

Thanks for this info and the link. I couldn't find this on the linked  
page, but what is an expression to get the current date minus one  
month, in the same format as above?

- Dave

> **···**
>
> On Sep 11, 9:38 pm, "DanDiebolt.exe" \<dandieb...@yahoo.com\> wrote:
> 
> > [Note: parts of this message were removed to make it a legal post.]
> > 
> > irb(main):005:0\> Date.today.to\_s  
> > =\> "2008-09-11"
> > 
> > Seehttp://www.ruby-doc.org/core/classes/Date.html

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [12 September 2008 18:06 UTC](https://rubytalk.org/t/getting-current-date/49021/4 "2008-09-12T18:06:28Z")

</div>

In IRB...

require 'date'  
=\> true  
(Date.today \<\< 1).to\_s  
=\> 2008-8-12  
#Be prepared for edge cases...  
(Date.new(2008-3-31) \<\< 1).to\_s  
=\> 2008-2-29

Todd

> **···**
>
> On Fri, Sep 12, 2008 at 12:13 PM, laredotornado \<laredotornado@zipmail.com\> wrote:
> 
> > On Sep 11, 9:38 pm, "DanDiebolt.exe" \<dandieb...@yahoo.com\> wrote:
> > 
> > > [Note: parts of this message were removed to make it a legal post.]
> > > 
> > > irb(main):005:0\> Date.today.to\_s  
> > > =\> "2008-09-11"
> > > 
> > > Seehttp://www.ruby-doc.org/core/classes/Date.html
> > 
> > Thanks for this info and the link. I couldn't find this on the linked  
> > page, but what is an expression to get the current date minus one  
> > month, in the same format as above?
> > 
> > - Dave

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [12 September 2008 18:24 UTC](https://rubytalk.org/t/getting-current-date/49021/5 "2008-09-12T18:24:14Z")

</div>

rails's activesupport library has hammered on these issues extensively  
- it's worth not reinventing the wheel

[http://as.rubyonrails.com/](http://as.rubyonrails.com/)

You can always copy bits of code if you don't want to pull the library in.

martin

> **···**
>
> On Fri, Sep 12, 2008 at 10:13 AM, laredotornado \<laredotornado@zipmail.com\> wrote:
> 
> > Thanks for this info and the link. I couldn't find this on the linked  
> > page, but what is an expression to get the current date minus one  
> > month, in the same format as above?

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [12 September 2008 18:45 UTC](https://rubytalk.org/t/getting-current-date/49021/6 "2008-09-12T18:45:51Z")

</div>

My clunky way:

n = Time.now  
d = [Time.gm](http://Time.gm)(n.year, n.month - 1, n.day)

p d

=\> Tue Aug 12 00:00:00 UTC 2008

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [14 September 2008 15:30 UTC](https://rubytalk.org/t/getting-current-date/49021/7 "2008-09-14T15:30:25Z")

</div>

\<cough/\> That was, of course supposed to be (Date.new(2008, 3, 31) \<\<  
1).to\_s, \_not\_ Date.new(2008-3-31).

Note that the method is leap-year aware.

Todd

> **···**
>
> On Fri, Sep 12, 2008 at 1:13 PM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > On Fri, Sep 12, 2008 at 12:13 PM, laredotornado \> \<laredotornado@zipmail.com\> wrote:
> > 
> > > On Sep 11, 9:38 pm, "DanDiebolt.exe" \<dandieb...@yahoo.com\> wrote:
> > > 
> > > > [Note: parts of this message were removed to make it a legal post.]
> > > > 
> > > > irb(main):005:0\> Date.today.to\_s  
> > > > =\> "2008-09-11"
> > > > 
> > > > Seehttp://www.ruby-doc.org/core/classes/Date.html
> > > 
> > > Thanks for this info and the link. I couldn't find this on the linked  
> > > page, but what is an expression to get the current date minus one  
> > > month, in the same format as above?
> > > 
> > > - Dave
> > 
> > In IRB...
> > 
> > require 'date'  
> > =\> true  
> > (Date.today \<\< 1).to\_s  
> > =\> 2008-8-12  
> > #Be prepared for edge cases...  
> > (Date.new(2008-3-31) \<\< 1).to\_s  
> > =\> 2008-2-29

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [12 September 2008 18:59 UTC](https://rubytalk.org/t/getting-current-date/49021/8 "2008-09-12T18:59:59Z")

</div>

ok. Improvement time:

n = Time.now  
if n.month == 12  
&nbsp;&nbsp;d = [Time.gm](http://Time.gm)(n.year - 1, 12, n.day)  
else  
&nbsp;&nbsp;d = [Time.gm](http://Time.gm)(n.year, 1 - 1, n.day)  
end

There has GOT to be a better way but I did not want you to have nothing.  
🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [12 September 2008 19:00 UTC](https://rubytalk.org/t/getting-current-date/49021/9 "2008-09-12T19:00:41Z")

</div>

dang typos

n = Time.now  
if n.month == 1  
&nbsp;&nbsp;d = [Time.gm](http://Time.gm)(n.year - 1, 12, n.day)  
else  
&nbsp;&nbsp;d = [Time.gm](http://Time.gm)(n.year, 1 - 1, n.day)  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [12 September 2008 19:02 UTC](https://rubytalk.org/t/getting-current-date/49021/10 "2008-09-12T19:02:25Z")

</div>

I put the testing code in and I feel REALLY stupid. If it is still  
wrong, I am going to hide under a rock until everyone forgets that I did  
this.

n = Time.now  
if n.month == 1  
&nbsp;&nbsp;d = [Time.gm](http://Time.gm)(n.year - 1, 12, n.day)  
else  
&nbsp;&nbsp;d = [Time.gm](http://Time.gm)(n.year, n.month - 1, n.day)  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [14 September 2008 13:45 UTC](https://rubytalk.org/t/getting-current-date/49021/11 "2008-09-14T13:45:37Z")

</div>

This also doesn't handle edge cases like March 30/31, or October 31.

> **···**
>
> On Fri, Sep 12, 2008 at 3:02 PM, Lloyd Linklater \<lloyd@2live4.com\> wrote:
> 
> > I put the testing code in and I feel REALLY stupid. If it is still  
> > wrong, I am going to hide under a rock until everyone forgets that I did  
> > this.
> > 
> > n = Time.now  
> > if n.month == 1  
> > d = Time.gm(n.year - 1, 12, n.day)  
> > else  
> > &nbsp;&nbsp;d = Time.gm(n.year, n.month - 1, n.day)  
> > end
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
