I am regularly scraping and storing some dated data from a website into a rails app, but the dates given on the website are in the following format:
11/25/2012 01
Which is the date and hour.
I need to write some code to convert this into the default DateTime format:
YYYY-MM-DD HH:MM:SS
but I’m new to Ruby.
Here is the code I have currently scraping the date-containing element with Nokogiri and extracting the text, maybe you could build off of this:
datedata = tr.css('td')[0].text
This scrapes the date table row and extracts the text. E.g., datedata = "11/25/2012 01"
Is there a method that can convert datedata into default DateTime format for saving into a database? FYI – Hours are the only time given, and they are necessary data for my use-case, no minutes or seconds.
You can use
DateTime‘sstrptimefunctionThis should be enough to be passed to ActiveRecord, but if you want to convert this to the SQL datetime string you mentioned in your question, you can use Rails
to_formatted_swith the:dboption.