How to compare two dates in a specific format ?

Hello,

I am not able to find how to compare two dates in '%Y-%m-%d %H:%M:%S,%3N' format

i have to two dates as below

date1="2016-08-24 09:47:40,444"
date2="2016-08-24 10:45:40,567"

how to compare these two dates ?

I have tried below without success

if [ "$date1" < "$date2" ];  then  echo 'yes'; fi

if [ "$date1" -le "$date2" ];  then  echo 'yes'; fi

Please suggest.

Thanks

To the shell, < is the input redirection operator. To use it in comparisons, you need to escape it:

 if [ "$date1" \< "$date2" ]; then echo 'yes'; fi
yes

Or even

if [ "$date1 < $date2" ]; then echo 'yes'; fi
yes
1 Like

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:

# printf "%(%s)T\n" now
1472042505

I hope this helps.

bakunin

Hello Ramneekgupta91,

In addition to what Bakunin had mentioned already, you could try following too in BASH, by converting time to epoch time and then calculate.

cat script.ksh
date1="2016-08-24 09:47:40,444"
date2="2016-08-24 10:45:40,567"
VAL1=$(date +%s -d"$date1")
VAL2=$(date +%s -d"$date2")

if [[ $VAL1 < $VAL2 ]]
then
        echo "Yes"
else
        echo "No"
fi

Thanks,
R. Singh

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.

Best regards
Peasant.

Hello RudiC

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.

Thanks

1 Like

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.

1 Like