1

I need to parse a date from excel file, it's in format 14-3-17 because excel changes it to that...

What is the best way to parse it to a valid date?

'14-03-17'.to_date #=> Sat, 17 Mar 0014
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • Check this http://stackoverflow.com/questions/2955830/checking-to-see-if-a-string-is-a-valid-date-ruby-on-rails – Salil Feb 13 '17 at 08:10

2 Answers2

4

You can use Date.strptime to parse as well as format the parsed date, take a look here to find the list of formatting options available.

Here's an example:

Date.strptime("14-03-17", "%d-%m-%y")
=> Tue, 14 Mar 2017

Goodluck.

sghosh968
  • 591
  • 6
  • 12
1

Use Date.parse:

Date.parse('14-3-17'.split('-').reverse.join('-'))
#=> Tue, 14 Mar 2017
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145