Re: get first and last date of a month (826 Views)
Reply
Frequent Advisor
viseshu
Posts: 54
Registered: ‎09-20-2006
Message 1 of 7 (826 Views)
Accepted Solution

get first and last date of a month

hi,
i need to pass two parameters to an sql which will be called from shell script.
First parameter is the first date of that month.
Second parameter is the last date of that month.

How can i obtain last date of that particular month in which the script runs????
Honored Contributor
Yang Qin_1
Posts: 514
Registered: ‎10-23-2002
Message 2 of 7 (826 Views)

Re: get first and last date of a month

The easy way is that you hard-code those last date because for Jan. Mar. Apr. May. Jun. Jul. Aug. Sep. Oct. Nev. Dec. the last day is fixed. For Feb. you may need to check on Calendar however, most of time it is 28.

So, you just need to check the current month then look up in you hard-coded last date.

Yang
Occasional Visitor
Chandrakanth
Posts: 1
Registered: ‎09-28-2006
Message 3 of 7 (826 Views)

Re: get first and last date of a month

Hi

to get the last date of the particular month
the below one line script works

echo `cal 4 2005`| awk '{print $NF}'
This gives the last last day of the April 2005 so you can pass the month and year as arguments if required..
for the current month
echo `cal`| awk '{print $NF}'

For the first date of the month it is no doubt that every month starts with 1st
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 4 of 7 (826 Views)

Re: get first and last date of a month

Hi,
please see here for a number of solutions for a similar problem:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=237796

cal and awk
or
caljd.sh by A. Clay
Honored Contributor
Bill Hassell
Posts: 14,210
Registered: ‎05-29-2000
Message 5 of 7 (826 Views)

Re: get first and last date of a month

Here's a script that just uses cal with some simple tools to extract:

- name of the month (October)
- day of the week for 1st day (Sun)
- last day number in the month (31)
- day of the week for the last day (Tue)

given month and year.
-------------------------------------

#!/usr/bin/sh
# usage: firstlast MM YY (or YYYY)

export PATH=/usr/bin
set -A DAY Sun Mon Tue Wed Thu Fri Sat
MYNAME=${0##*/}

function Usage
{
[ $# -gt 0 ] && echo "\n$@"
cat << EOF

Usage: $MYNAME MM YY
where: MM = month number (1-12)
and: YY = year (2 or 4 digits)

EOF
exit
}

[ $# -ne 2 ] && Usage
MON=$1
[ $MON -lt 1 -o $MON -gt 12 ] && Usage "Month not 1-12"
YEAR=$2
[ $YEAR -lt 100 ] && let YEAR=YEAR+2000
MONTHNAME=$(cal $MON $YEAR | head -1 | awk '{print $1}')

# WKQTY is the number of elements on the week line
WKQTY=$(cal $MON $YEAR | head -3 | tail -1 | wc -w)

# The number of days in the first week for cal will define
# which day the 1st falls on. Just subtract the $WKQTY from
# 7 and that will be the array index for the weekday
let WKDAYINDEX=7-$WKQTY
WKDAY1ST=${DAY[$WKDAYINDEX]}

LASTDAY=$(echo $(cal $MON $YEAR) | awk '{print $NF}')
# WKQTY is the number of elements in the last week
# Subtract 1 to get the index for day of week $DAY
WKQTY="$(cal $MON $YEAR | grep $LASTDAY | wc -w)"
let WKDAYINDEX=$WKQTY-1
WKDAYLAST=${DAY[$WKDAYINDEX]}

echo "\nmonth $MONTHNAME $YEAR"
echo " first day 1 is $WKDAY1ST"
echo " last day $LASTDAY is $WKDAYLAST"
echo
cal $MON $YEAR

-------------------------------------
No need to use all the code - just extract the portion that you need (such as $LASTDAY=)
Respected Contributor
David Bellamy
Posts: 211
Registered: ‎06-29-1999
Message 6 of 7 (826 Views)

Re: get first and last date of a month

Here is a Perl script that will work

#!/usr/bin/perl
use Date::Manip;

$now=&UnixDate("today","%m/%d/%Y");

@years=(qw/2004 2005 2006/);
foreach $years(@years) {
print("The Following is the Last Day of the Months for $years\n");
foreach(qw/January February March April May June July August September October
November December/) {
$date1=&UnixDate("last day in $_ $years","%m/%d/%Y");
print("$date1\n");
if($date1=~/$now/) {
pass $date1 to sql statement
}

Hope this helps

Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 7 of 7 (826 Views)

Re: get first and last date of a month

try setting these variables in your shell script to get the first and last day of the month:

firstDay=$(cal | awk 'BEGIN{RS=""}{print $10}')

lastDay=$(cal | awk 'BEGIN{RS=""}{print $NF}')

~hope it helps
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.