To compare two dates the most common procedure is to convert both to some integer value and then compare these. Most commonly in UNIX and UNIX-like systems the conversion is done to seconds passed since Jan 1st, 1970, 0:00 am (the so-called "epoch" or "unix time"). For instance, as i am writing this this time value is "1472042505". It makes it easy to calculate a duration by simply subtracting one date in this form from the other.
There are many ways of converting time values to unix time format, here is one in Korn Shell 93 using the printf command:
Good tip also, if the date is in format specified : YYYY MM DD ...<everything else>
A simple numeric comparison on integer value would suffice -gt, -lt ,-eq , if you do not wish to use epoch or date utility.
Of course, one would need to strip out the input not required (leave only numbers), which should not be too much of a problem.
Thanks for your response. But your second suggestion will not work correctly. It will give the output yes in any case irrespective of the condition is true or false.
With the format of the strings you are comparing, a string comparison is sufficient, but that type of comparison is not supported by the standard test expression and [ expression ] utilities. If you're using a recent bash or a 1993 or later version of ksh as your shell, you could use the conditional expression (as suggested by RavinderSingh13 that has been added to the syntax of their command languages:
if [[ $date1 < $date2 ]]; then echo yes; fi
or more simply:
[[ $date1 < $date2 ]] && echo yes
This should work fine up to the point that you want to compare a date in the year 9999 to a date in the year 10000.
Note that the command Ravinder suggested:
if [[ $VAL1 < $VAL2 ]]
then
echo "Yes"
else
echo "No"
fi
is performing a string comparison (even though VAL1 an VAL2 have been set to numeric strings. This will work as long as both numeric strings contain the same number of digits, but is dangerous in the general case. When comparing numeric strings it is safer to use the numeric comparison operators ( -lt , -le , -eq , -ge , -gt , and -ne ) instead of the string comparison operators ( < , <= , == , >= , > , and != ) available in the [[ expression ]] syntax.