# How to get date out of datetime field?

**URL:** <https://www.ruby-forum.com/t/how-to-get-date-out-of-datetime-field/122557>\
**Category:** Ruby\
**Created:** [December 9, 2007, 11:21am UTC](https://www.ruby-forum.com/t/how-to-get-date-out-of-datetime-field/122557 "2007-12-09T11:21:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vapor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@vapor](https://www.ruby-forum.com/u/vapor)\
**Post date:** [December 9, 2007, 11:21am UTC](https://www.ruby-forum.com/t/how-to-get-date-out-of-datetime-field/122557/1 "2007-12-09T11:21:27Z")

</div>

I have a field in db with datetime format and it contains more than just  
simple date. How can I get only date from it?

---

<div class="post-metadata">

**Author:** ![vapor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@vapor](https://www.ruby-forum.com/u/vapor)\
**Post date:** [December 9, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/how-to-get-date-out-of-datetime-field/122557/2 "2007-12-09T20:06:21Z")

</div>

On Dec 9, 3:21 am, “Vapor …” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a field in db with datetime format and it contains more than just  
> simple date. How can I get only date from it?

What does your format look like?  
What database are you using?  
How are you connecting Ruby to the database? Are you using  
ActiveRecord? Something else?

Do you already have instances of a Ruby Time class? DateTime class?

Do you want an instance of a Date class? Do you just want to visually  
display the date only information?

More information required…

---

<div class="post-metadata">

**Author:** ![vapor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@vapor](https://www.ruby-forum.com/u/vapor)\
**Post date:** [December 10, 2007, 6:20am UTC](https://www.ruby-forum.com/t/how-to-get-date-out-of-datetime-field/122557/3 "2007-12-10T06:20:13Z")

</div>

Gavin K. wrote:

> On Dec 9, 3:21 am, “Vapor …” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have a field in db with datetime format and it contains more than just  
> > simple date. How can I get only date from it?
> 
> What does your format look like?  
> What database are you using?  
> How are you connecting Ruby to the database? Are you using  
> ActiveRecord? Something else?
> 
> Do you already have instances of a Ruby Time class? DateTime class?
> 
> Do you want an instance of a Date class? Do you just want to visually  
> display the date only information?
> 
> More information required…

oh well

I am using MySQL and when I look into it, it shows something like  
‘2007-12-10 18:58:17’ like this.

And I am using all the default settings (ActiveRecord and stuff)

---

<div class="post-metadata">

**Author:** ![vapor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@vapor](https://www.ruby-forum.com/u/vapor)\
**Post date:** [December 10, 2007, 7:25am UTC](https://www.ruby-forum.com/t/how-to-get-date-out-of-datetime-field/122557/4 "2007-12-10T07:25:22Z")

</div>

You probably look for something like this:

myDate.strftime(’%Y%m%d’)

by  
TheR
