Forum: Ruby Compare mm/dd/yy dates

7bb2a94f867841b58214be09992831d3?d=identicon&s=25 Fosiul Alam (fosiul)
on 2014-03-23 19:30
Hi
i am trying to write script with needs to compare couple of dates
example :-

                                             Protection
Good   [000001L5]     [MSL001:  1]      No   04/22/14 18:11:06

Good   [A00033L5]     [MSL001:    25]   Yes  03/28/14 03:18:07
Good   [A00084L5]     [MSL001:    30]   Yes  03/30/14 03:00:25


now i need to eject the tape which has longer Protection which is
"04/22/14"

now how i compare 04/22/14 from 03/28/14 and 03/30/14

Do i have to convert mm/dd/yy to Unix date then need to compare ? or
Ruby can company it as it is ??

Please advise.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-03-24 23:20
Fosiul Alam wrote in post #1140705:

> Do i have to convert mm/dd/yy to Unix date then need to compare ? or
> Ruby can company it as it is ??
>



irb(main):021:0> require 'time'
=> true
irb(main):028:0> Time.parse("03/03/14")
=> 2003-03-14 00:00:00 +0100
irb(main):029:0> Time.parse("14/03/30")
=> 2014-03-30 00:00:00 +0100

So something like that should work :

File.read(filename).split(/\r?\n/).sort_by {|line|
  d= line.scan(/\d\d\/\d\d\/\d\d/).first.scan(/\d\d/).reverse.join('/')
  Time.parse( d )
}.last
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.