# Datetime format

**URL:** <https://www.ruby-forum.com/t/datetime-format/139590>\
**Category:** Rails\
**Created:** [June 5, 2008, 8:51am UTC](https://www.ruby-forum.com/t/datetime-format/139590 "2008-06-05T08:51:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [June 5, 2008, 8:51am UTC](https://www.ruby-forum.com/t/datetime-format/139590/1 "2008-06-05T08:51:07Z")

</div>

These code is to read the email i save in the text file that i have  
retrieved from gmail.

msisdn = “”  
datetime = “”  
date = “”  
File.open(Pathname.new(ARGV[0]), ‘r’) do |mbox|  
RMail::Mailbox.parse\_mbox(mbox) do |raw|  
count += 1  
print “Mail #{count}\n”  
message = RMail::Parser.read(raw)

```
msisdn = message.header['From']
datetime = message.header['Date']

```

When i do  
date = DateTime.parse(datetime)  
the result is 2008-06-05T08:45:58+00:00

But i only want the format to be 2008-06-05 08:45:58 because only these  
match the mysql datetime format. Is there other way of do the parsing?  
Thanks

Wawa

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [June 5, 2008, 11:12am UTC](https://www.ruby-forum.com/t/datetime-format/139590/2 "2008-06-05T11:12:22Z")

</div>

Emmie W. wrote:

> But i only want the format to be 2008-06-05 08:45:58 because only these  
> match the mysql datetime format. Is there other way of do the parsing?  
> Thanks
> 
> Wawa

You can output the datetime in any format you desire using the strftime  
method. Off the top of my head, something like this may do the trick:

date.strftime(’%Y-%m-%d %H:%M:%S)

---

<div class="post-metadata">

**Author:** ![wawa88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5a626/32.png) [@wawa88](https://www.ruby-forum.com/u/wawa88)\
**Post date:** [June 5, 2008, 11:24am UTC](https://www.ruby-forum.com/t/datetime-format/139590/3 "2008-06-05T11:24:22Z")

</div>

for usage in db you can do:

date.to\_s(:db)

this will output any DateTime for use with SQL
