Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

get first and last date of a month

SOLVED
Go to solution
viseshu
Frequent Advisor

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????
6 REPLIES
Yang Qin_1
Honored Contributor

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
Highlighted
Chandrakanth
Occasional Visitor
Solution

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
Peter Godron
Honored Contributor

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
Bill Hassell
Honored Contributor

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=)


Bill Hassell, sysadmin
David Bellamy
Respected Contributor

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

Sandman!
Honored Contributor

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