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!
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!
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)>
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
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