Search code examples
ruby-on-rails-3datetimedatetimepickerdatetime-format

Rails 3 Convert given days of duration to datetime


I am trying to convert a given amount of days in a form to a datetime format.

I have a field that users can put how many days they want the duration of the object to last. so for example they can type: 90 and that will convert the integer '90' to actual 90 days later in datetime format to my db. I also need to refuse strings, and make sure it always is an integer. If someone types in 90 Days, it removes the Days part and saves the integer.

I dont need the datetime to be displayed though, just need to have it convert for the database since it is a datetime column. This may be tricky though cause I also need it to display the integer if they wish to edit the number. So it would have to go both ways.

any help would be greatly appreciated :)

P.S. my datetime formats are yyyy-dd-mm

Thanks!


Solution

  • thanks to active-support, there are a lot of additions to the date class in rails.

    what you need is to use the extensions to the number class. these enable you to write stuff like:

    90.days.from_now
    

    you would need to store this number AND the creation time of when the user sets this in order to convert or change the duration.