Treating string as date ?

Is there a way to treat a string as date and compare it to the current date?

lets assum inpu lik

$ cat myfile
 
Name   Last login
**************************
Sara      2/13/2012
kalpeer   2/15/2012
ygemici   2/14/2012

we want to display the name who logged in during the last # of days.

"`date +%D`" will give us the day of the year ( which is 043 for today)
is there a method to compare the dates in the input to the current day?

Hi,
Below command will give you the date in the format of 02/15/2012

Check this thread.

Thanks,
Kalai

Thanks Kalai, but my aim is not get this format. It is to read the string in the input file as a date and compare it to the current actual date.

Something like this:

#! /bin/bash

while read x y
do
    if [ `date -d "$y" +%j` -le `date +%j` ]
    then
        echo "Less than or equal to curr date"
    else
        echo "Greater than curr date"
    fi
done < myfile
1 Like

You could parse the date like this

$ cat compare_dates.ksh
#!/bin/ksh

USERDATE="2/15/2012"
IFS=/
set -- $USERDATE
typeset -Z2 MONTH=$1
typeset -Z2 DAY=$2
YEAR=$3
MYDATE="$YEAR$MONTH$DAY"
echo $MYDATE

TODAY=$(date +%Y%m%d)

if [ $TODAY -eq $MYDATE ]; then
     echo "We have a match!"
fi

exit 0

$ ./compare_dates.ksh
20120215
We have a match!