RE: Last Sunday of the Month

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 06:59 AM 4/5/2005, you wrote:
> On Monday 04 April 2005 12:55 pm, Steve Buehler wrote:
> > RHEL ES 3
> > I can't seem to find this about the date command, but that doesn't
> > mean it can't do it.  I have overlooked things in the past.
> >
> > 1.  Can the date command give you a date for the Last Sunday of a
> > month?  Or do I have to go through a loop to check each day of the
> > month to see if it is the last Sunday of the month?
> >
> > 2.  I can do a date --date="last sunday".  Is there a way
> to tell it
> > to give me the date of 4 Sundays ago?  I tried date
> --date="4 sundays
> > ago" and that didn't do it.
>
> You can use -d to request a date by string, but i think it
> only uses 'next' or 'last'.
> date -d "last month"
> Fri Mar  4 21:00:09 PST 2005
>
> date -d "next sunday"
> Sun Apr 10 00:00:00 PDT 2005
>
> You could check if next sunday is the current month:
> date -d "next sunday"|awk ' {print $2} ...does it = $thismonth?
> That would be a lot easier to check than checking every day.
>
> --

Yes, check if next sunday is the current month but get the date like this:

LASTSUNDAY=`date -d "last sunday"`
NEXTSUNDAY=`date -d "next sunday"`
NEXTSUNDAYMONTH=`date -d "$NEXTSUNDAY" | awk '{print $2}'`

That will allow you to iterate in a while loop with the following (i.e. if
NEXTSUNDAYMONTH is not equal to the current month):

LASTSUNDAY=$NEXTSUNDAY
NEXTSUNDAY=`date -d "$NEXTSUNDAY + 1 week"`      # I guess this line is what
you're looking for
NEXTSUNDAYMONTH=`date -d "$NEXTSUNDAY" | awk '{print $2}'`


When NEXTSUNDAYMONTH is no longer equal to the current month, LASTSUNDAY will contain the date of the last Sunday of the current month.

Thanks To all who answered. I came up with the following function since apparantly it can't be done with just the date command.:
NEXTMONTH=`/bin/date --date="next month" +%m`
g_1snm()
{
if [ $NEXTMONTH == `date --date="$1 sunday" +%m` ]
then
a=`expr $1 - 1`
LASTSUNDAYTHISMONTH=`date --date="$a sunday" +%Y%m%d`
else
g_1snm `expr $1 + 1`
fi
}
g_1snm 1


--
redhat-list mailing list
unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list

[Index of Archives]     [CentOS]     [Kernel Development]     [PAM]     [Fedora Users]     [Red Hat Development]     [Big List of Linux Books]     [Linux Admin]     [Gimp]     [Asterisk PBX]     [Yosemite News]     [Red Hat Crash Utility]


  Powered by Linux