Operating System - Linux
1748046 Members
4957 Online
108757 Solutions
New Discussion юеВ

Re: 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 6
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
Chandrakanth
New Member
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