- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: get first and last date of a month
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 07:02 PM
тАО10-12-2006 07:02 PM
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????
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 07:20 PM
тАО10-12-2006 07:20 PM
Re: get first and last date of a month
So, you just need to check the current month then look up in you hard-coded last date.
Yang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 08:58 PM
тАО10-12-2006 08:58 PM
Solutionto 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
- Tags:
- cal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2006 09:03 PM
тАО10-12-2006 09:03 PM
Re: get first and last date of a month
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
- Tags:
- caljd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 04:49 AM
тАО10-13-2006 04:49 AM
Re: get first and last date of a month
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 04:58 AM
тАО10-13-2006 04:58 AM
Re: get first and last date of a month
#!/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
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-13-2006 06:41 AM
тАО10-13-2006 06:41 AM
Re: get first and last date of a month
firstDay=$(cal | awk 'BEGIN{RS=""}{print $10}')
lastDay=$(cal | awk 'BEGIN{RS=""}{print $NF}')
~hope it helps