Importing dates from spreadsheets in Ruby
I encountered an interesting issue yesterday. While importing data present in an xlsx file, I noticed that dates present in the excel file were being imported as numerical floats. My original code below
DATE_COL = 10
2.upto(10) do |i|
...
excel_file = Excelx.new("#{Rails.root}/temp/final_cand/khi_counsel_list.xlsx")
date_ar = excel_file.cell(i, DATE_COL).to_formatted_s(:long)
end
After a quick google, I found out that the float being returned was the number of days that have elapsed since (Dec 30, 1899). As to why this is the case, I have no clue and didn't look it up.
I went ahead and modified my code to the following after which everything started working fine.
date_ar = (DateTime.new(1899,12,30) + excel_file.cell(i, DATE_COL).days).to_date.to_formatted_s(:long)