Strftime of datetime

12,182

This should not be necessary, but as a workaround you could try this:

Time.at(
    Date.today.to_time.to_i + warning.notify_time_close.seconds
).strftime("%d-%m-%Y %H:%M:%S")
Share:
12,182
Twiddr
Author by

Twiddr

Newbie trying to learn Ruby on Rails!

Updated on June 04, 2022

Comments

  • Twiddr
    Twiddr almost 2 years

    I got the below code which I would like to convert to a string in a specific format. But I can't get it working.

    Date.today + warning.notify_time_close.seconds
    

    What I want to do is somethin like this but it doesn't work :)

    (Date.today + warning.notify_time_close.seconds).strftime "%d-%m-%Y %H:%M:%S"
    

    I know its simple but just don't know how to do it :)

    Thanks in advance!

    Kenneth

    • Max Williams
      Max Williams over 13 years
      What does warning.notify_time_close return? If it's an int then the above should work. I'm guessing it isn't.
    • Mischa
      Mischa over 13 years
      If you do Date.today + 3 it adds three days, not three seconds
    • Twiddr
      Twiddr over 13 years
      Its an int.. and it works in the console.. but im using it in an RSS builder.. It get this error: undefined method `strftime' for #<String:0x103432430>
  • Twiddr
    Twiddr over 13 years
    Doesn't work either.. :) Still gets an error: undefined method `strftime' for #<String:0x103629d38>
  • Mischa
    Mischa over 13 years
    Well then warning.notify_time_close.seconds is a String somehow... what happens if you try warning.notify_time_close.seconds.to_i?
  • Twiddr
    Twiddr over 13 years
    Still the same.. Even get the error if i replace warning.notify_time_close with 100.. Its all going on in a feed.rss.builder file - could that be an issue?
  • Twiddr
    Twiddr over 13 years
    This one works: Date.today.to_time.strftime("%d-%m-%Y %H:%M:%S").. But doesn't give me the right result..
  • Mischa
    Mischa over 13 years
    I posted a workaround.. not sure why the stuff in the braces get converted to a String