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
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")
A little shorter
t = Time.now
puts t.strftime("%F %T")
=> "2015-01-06 14:01:05"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With