How can I check whether $date1 is before $date2?

Both dates are in DD/MM/YY format i.e. 01/01/15.

  • 1
    Where are these dates coming from? That's an unusual format for dates to be in, and I wonder if it's dependent on your locale setting.
    – Random832
    Oct 16, 2015 at 19:24
  • 1
    huh? DD/MM/YY is a perfectly normal date format, used in almost every country except US and Japan, especially for hand-written or manually entered dates. Yes, YYYYMMDD makes more sense but getting people to switch is only slightly more likely than getting americans to use metric. or to stop using the insane MM/DD/YY.
    – cas
    Oct 16, 2015 at 21:57

2 Answers 2


if you wanted to be real cheeky you could always convert the dates based off the epoch 1970-01-01 00:00:00 UTC.

#added to fix DD/MM/YY format
input1=`echo $1 | awk -F "/" '{print $2"/"$1"/"$3}'`
input2=`echo $2 | awk -F "/" '{print $2"/"$1"/"$3}'`

date1=`date +%s --date="$input1"`
date2=`date +%s --date="$input2"`
if [[ "$date1" -lt "$date2" ]]; then
    echo "$1 earlier than $2"
    echo "$1 not earlier than $2"
  • 2
    That isn't cheeky. That's the correct way to deal with dates.
    – Tom Hunt
    Oct 16, 2015 at 21:42
  • Ah, good to know. I'm relatively inexperienced and really only tinker to make life easier at work, so it's good to know that at least one of my methods was correct after getting bashed about others.
    – Kip K
    Oct 16, 2015 at 21:53
  • 1
    The problem is that GNU date does not let you input in DD/MM/YY format. Try date +%s --date="16/10/15" Oct 16, 2015 at 23:29

First split it into fields, since you'll need to rearrange them. YYMMDD or YYYYMMDD is much better for this kind of comparison operation since it can look at the whole string at once.

IFS="/" date1a=($date1) date2a=($date2)

Then, if you need to deal with years before 1999, fix the two-digit years to be four digits, otherwise 99 is greater than 01.

((date1a[2] += (date1a[2] < 70 ? 2000 : 1900)))
((date2a[2] += (date2a[2] < 70 ? 2000 : 1900)))

Then put them back together in YYYYMMDD order:


And now you can compare them:

if (( $date1b < $date2b )); then
    echo "earlier"
    echo "later"

Note: This is a bash-specific answer, depending on features specific to bash (and ksh) not specified in POSIX, and so may not work with /bin/sh or the default shell on some systems.

POSIX requires a different approach; you can rely on awk to do some of the heavy lifting:

awkscr='BEGIN {FS="/"}; {print $3+($3<70?2000:1900) $2 $1}'
date1b=$(echo "$date1" | awk "$awkscr")
date2b=$(echo "$date2" | awk "$awkscr")
if [ "$date1b" -lt "$date2b" ]; then
    echo "earlier"
    echo "later"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .