1823166 Members
3782 Online
109647 Solutions
New Discussion юеВ

Last Day of Month?

 
SOLVED
Go to solution
John Wolfe_1
Advisor

Last Day of Month?

Hello,

I need to be able to tell inside a script if the current date is the last day of the month. I also need it to work during leap years.
Any thoughts?

TIA, John
At least I have a job.
13 REPLIES 13
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Last Day of Month?

Hi John:

This sounds like a job for my universal date hammer, caljd.sh.

if [ "$(caljd.sh -M)" != "$(caljd.sh -n 1 -M)" ]
then
echo "Last Day of the Month"
fi

Basically, this compares today's month with that of tomorrow. If they are different then it must be the last day of the month. Invoke caljd.sh -u for full usage.

Regards, Clay
If it ain't broke, I can fix that.
SHABU KHAN
Trusted Contributor

Re: Last Day of Month?

John,

Clay's caljd.sh is SUPER, there is another work around if you like ..

These are from my old notes, not tested ...

-----------------
The idea is to check whether
*tomorrow* is the first day of a month.

using date, change MET to your local time zone

#!/bin/ksh
if test `TZ=MET-24 date +%d` = 1; then
# today is the last day of month
print "Today is the last day of the month"
else
print "Today is NOT the last day of the month"
fi

-------------
Perl Method

#!/usr/bin/perl -w
#
# last-day-of-month - check if today is the last day of a month
#
# Input: none.
# Output: none.
# Exit status: 0 (true) if today is the last day in a month, otherwise 1.
# Algorithm: Get localtime and advance the day of month by one. Let mktime
# normalize the result and check whether day of month became 1.
# Requires: perl5.
#

use POSIX;

@the_time = localtime (time);
++$the_time[3]; # Element 4 is the day of month [1..31]
if ((localtime (POSIX::mktime (@the_time)))[3] == 1) {
exit 0;
}
exit 1;


------------
Hope this helps !

Thanks,
Shabu
SHABU KHAN
Trusted Contributor

Re: Last Day of Month?

John,

Also there is another trick that I just found out using the cal (print calendar) command

-------------

#!/usr/bin/ksh

LASTDAY=`cal | sed '/^$/d' | tail -1 | head -1 | awk '{print $NF}'`
echo $LASTDAY

CURR_DATE=`date +%d`
echo $CURR_DATE

if [[ "${CURR_DATE}" -eq "${LASTDAY}" ]]; then
print "Today is the last day of the month"
#do your stuff
else
print "Today is NOT the last day of the month"
fi

----------------

Thanks,
Shabu
H.Merijn Brand (procura
Honored Contributor

Re: Last Day of Month?

perl -le '(localtime time+86400)[3]==1&&print"last"'
Enjoy, Have FUN! H.Merijn
Carlos Fernandez Riera
Honored Contributor

Re: Last Day of Month?

for i in $(cal )
> do
> b=$i
> done; echo $b
unsupported
Ceesjan van Hattum
Esteemed Contributor

Re: Last Day of Month?

cal | awk '{prev=last; last=$0}END{print prev}' | awk '{print $NF}'

Regards,
Ceesjan
David Lodge
Trusted Contributor

Re: Last Day of Month?

>cal | awk '{prev=last; last=$0}END{print prev}' | awk '{print $NF}'

Ewww! too many awks; waste of processing time... You'll be using cat next!

Why don't you just use the Record Seperator field:

cal | awk 'BEGIN { RS='\n'} {print $NF}'

Bit shorter :-)

dave
James R. Ferguson
Acclaimed Contributor

Re: Last Day of Month?

Hi John:

...and here's one more 'awk' variation using 'cal':

# cal|awk 'NF>0 {D=$NF} END{print D}'

# cal 02 2000|awk 'NF>0 {D=$NF} END{print D}'

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Last Day of Month?

Hi, all of you sh/awk/sed/cut/caldj script kiddies :)

You realy should learn perl and be flexible. Let's say I want to know if it's only less than 5 day's till the end of the month? Can you do that with $TZ, cal, caldj?

l1:/u/usr/merijn 103 > perl -le '$n=shift;(localtime time+$n*86400)[3]<=$n&&print"the end is near"' 20
l1:/u/usr/merijn 104 > perl -le '$n=shift;(localtime time+$n*86400)[3]<=$n&&print"the end is near"' 21
the end is near
l1:/u/usr/merijn 105 >
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Last Day of Month?

Greetings Procura:

Yes, I can do that in caljd.sh, substitute a variable for -n 1 and you are there. Of course, a more robust version would have to compare both the month and the year in case your $n got very large. I only wish I were smart enough to code in Perl.

Now, can you do this in one line?
What is the date 90 days from now unless that happens to fall on a weekend or a holiday? (In that case skip to the next non-excluded day).
Oh, and while you are at it, print the output in YYYY/MM/DD format. Believe it or not, there was a posting with almost that very request and it turned out to be a one-liner for caljd.sh.

DT=$(caljd.sh -y -S "/" $(caljd.sh -n 90 -x 0 -x 6 -H))

Oh well, maybe I should have tried to use my itty-bitty knowledge of Perl for that.

Regards, Clay


If it ain't broke, I can fix that.
John Wolfe_1
Advisor

Re: Last Day of Month?

Hello everybody,

I used A. Clay's method. It worked the first time and then I printed the usage message and found many useful options for that script.

Thanks everybody for the quick help. I'm sorry it took so long to assign points. I was having a very difficult time using the forum today.

John W.
At least I have a job.
H.Merijn Brand (procura
Honored Contributor

Re: Last Day of Month?

Clay, sure I can :)

I know nothing about holidays, and I'm still not using a module. It's still one line of standard perl

l1:/tmp 118 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 90
Wed Jul 10 14:52:14 2002
l1:/tmp 119 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 91
Thu Jul 11 14:52:18 2002
l1:/tmp 120 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 92
Fri Jul 12 14:52:20 2002
l1:/tmp 121 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 93
Mon Jul 15 14:52:23 2002
l1:/tmp 122 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 94
Mon Jul 15 14:52:25 2002
l1:/tmp 123 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 95
Mon Jul 15 14:52:28 2002
l1:/tmp 124 > perl -le '$d=86400;$t=time+$d*shift;$t+=$d while(!((localtime$t)[6]%6));print scalar localtime$t;' 96
Tue Jul 16 14:52:30 2002
l1:/tmp 125 >
Enjoy, Have FUN! H.Merijn
Bill Hassell
Honored Contributor

Re: Last Day of Month?

Here's a generic script that works on all dates that cal understands:

#!/usr/bin/sh
# Report the numeric day of the last day
# of a particular month and year
# Usage: lastday [ month-number [ year ] ]

set -u
PATH=/usr/bin

# Setup defaults

MYNAME=${0##*/}
MONTH=$(date "+%m")
YEAR=$(date "+%Y")

# Process optional arguments

if [ $# -gt 0 ]
then
if [ $# -eq 1 ]
then
MONTH=$1
else
MONTH=$1
YEAR=$2
if [ ${#YEAR} -eq 2 ]
then
YEAR=$(( YEAR + 2000))
fi
fi
fi

# Find the last day of the month using cal

CALDAYS=$(cal $MONTH $YEAR 2> /dev/null)
if [ $? -ne 0 ]
then
echo "Bad Argument(s): $@"
echo "Usage: $MYNAME [ month [ year ] ]"
exit 1
else
LASTDAY=$(echo "$CALDAYS" | awk 'BEGIN { RS='\n'} {print $NF}' )
echo "For $MONTH/$YEAR, last day is $LASTDAY"
fi



Bill Hassell, sysadmin