date comparison in unix and identify the earlier one

57 views Asked by At

I have two dates to compare and find out the earlier one. ( using SunOS 5.10)

date1='01May2014'
date2='03Apr2014'

I need to determine the earlier date.

2

There are 2 answers

0
pradeep On BEST ANSWER

i used sql to solve since i dont find answer into unix.

select least((select to_date('01May2014','DDMONYYYY')from dual ), (select to_date('01Apr2014','DDMONYYYY') from dual )) from dual;

3
Sobrique On

You probably want:

date --date='01May2014' +"%Y%m%d"
date --date='03Apr2014' +"%Y%m%d"

Then you can sort numerically.

D1='01May2014'
D2='03Apr2014'

for D in $D1 $D2
do
    date --date=$D +"%Y%m%d"
done | sort | head -1