Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Time or DateTime to MySQL compatible DATETIME

According to "Ruby datetime suitable for mysql comparison", I should be able to do:

Time.now.to_s(:db)

This doesn't appear to be valid anymore. I get:

irb(main):001:0> Time.now.to_s(:db)
ArgumentError: wrong number of arguments (1 for 0)
        from (irb):1:in `to_s'
        from (irb):1
        from C:/Ruby22/bin/irb:11:in `<main>'

Does this functionality still exist or do I have to manually format the date and time to fit MySQL format?

I'm using ruby 2.2.2.

like image 878
PressingOnAlways Avatar asked Oct 21 '25 06:10

PressingOnAlways


1 Answers

Time#to_s doesn't accept arguments in Ruby. If you're using Rails, ActiveSupport::TimeWithZone supplies the to_s method you were referring to.

To get this format in Ruby without ActiveSupport you can use:

Time.now.strftime('%Y-%m-%d %H:%M:%S')
like image 75
Anthony Bobsin Avatar answered Oct 23 '25 22:10

Anthony Bobsin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!