-3

I have a column of dates in the format dd mmm'yy (ex: 26 Jun'17).These dates have to be checked if they are expired using Ruby.How can i do that?

Thanks!

user2993178
  • 97
  • 1
  • 12
  • Possible duplicate of [Ruby parse date string](http://stackoverflow.com/questions/21391953/ruby-parse-date-string) – Wand Maker Jun 28 '16 at 14:27
  • I've seen a lot of wild and zany date formats, but that one is truly perplexingly awkward. – tadman Jun 28 '16 at 15:46
  • I don't want to mark this a duplicate of the nominated exemplar, since that question is closed. I think this question is unclear, though. Do you want help with parsing the date string, or checking if the date is expired, or both? If you need help checking that it is expired, then what does "expired" mean? Does it mean that you need to know if the date is older than some constant date? Or that it is so-many days older than today? – Wayne Conrad Jun 28 '16 at 16:53

3 Answers3

2
d = "26 Jun'17"
#⇒ "26 Jun'17"
▶ Date.today >= Date.parse(d.tr("'", ' '))
#⇒ false

Date#parse understands “26 Jun 17”:

▶ Date.parse d.tr("'", ' ')
#⇒ #<Date: 2017-06-26 ((2457931j,0s,0n),+0s,2299161j)>
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Is "16 Jun 17" in 2016 or 2017? Double digit years are absurd for this reason. In the 1980s and 1990s you had the luxury of non-conflicting digits, but it won't be until 2032 that we're back on track there. – tadman Jun 28 '16 at 15:46
  • @tadman Well, the whole Rails Colossus is built on the basement of _conventions_. After all `01/02/2016` has the same ambiguity. `DateTime` treats `XX Jun YY` as “June, day XX, year YY of current century,” and we probably should just consider using it. – Aleksei Matiushkin Jun 28 '16 at 15:54
  • @tadman One should use digits for days, letters for months and romans for years: `17 June MMXVI`. – Aleksei Matiushkin Jun 28 '16 at 15:56
  • Maybe the Romans were on to something. – tadman Jun 28 '16 at 16:29
  • 1
    Maybe ISO-8601 is onto something – Wayne Conrad Jun 28 '16 at 16:50
2

You can use Date module from Ruby's standard library to parse your date strings and then compare them using <=>.

By the way, if you are working in a "pure" ruby environment (not Rails console) you'd have to include it into your code:

require 'date'

Date.parse("26 Jun'17") > Date.today               # => true
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
0

Convert it to a datetime object and then compare to other date time object?

time_one = DateTime.new(2001,2,3)
time_two = DateTime.new(2001,1,3)

time_one > time_two
Alex Harris
  • 6,172
  • 2
  • 32
  • 57