Tim Tim - 1 year ago 73
Ruby Question

Ruby - DateTime for Database

I have a Database column with the syntax "0000-00-00 00:00:00".
In PHP I would do
date('Y-m-d H:i:s');

In Ruby, I do

require 'date'
now = DateTime::now()
puts "#{now.year()}-#{now.mon()}-#{now.mday()} #{now.hour()}:#{now.min()}:#{now.sec()}"

The result is:
"2010-1-5 10:16:4"
That's not okay. How could I create a "timestring" with the format "0000-00-00 00:00:00"?

Thanks a lot in advance & Best Regards

Answer Source

You can format the dates like in PHP thanks to the built-in Time class, see documentation there.

This would give for your case :

t = Time.now
puts t.strftime("%Y-%m-%d %H:%M:%S")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download