- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: last day of previous 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
тАО11-06-2006 10:23 PM
тАО11-06-2006 10:23 PM
last day of previous month
I need to get the last day and first day of previous month.Can you please help me out
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-06-2006 10:38 PM
тАО11-06-2006 10:38 PM
Re: last day of previous month
Since you don't offer what language you want to use (Perl, shell, awk), I'll offer the general outline.
If the current month is one (Januaray=1) then the previous month is twelve (December=12) and its last day is 31. Exit.
Otherwise subtract one from the current month and using the resulting value, index into an array of days-in-a-month. If you point to February, do a modulus 4 (year mod 4) and if the result is zero (0) the last day of February is incremeted by one to equal 29.
The first day of any month is a degenerate case --- its simply one (1). You didn't specify if you wanted the dayname (e.g. Tuesday), etc.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-06-2006 10:51 PM
тАО11-06-2006 10:51 PM
Re: last day of previous month
can you please put it in awk???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-06-2006 11:20 PM
тАО11-06-2006 11:20 PM
Re: last day of previous month
cal 10|grep -v '[a-zA-Z]'|tail -1|awk '{print $NF}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-06-2006 11:26 PM
тАО11-06-2006 11:26 PM
Re: last day of previous month
for the last day of the previous month, a shell interpretation of mr. Ferguson:
#!/usr/bin/sh
CURRENT_MONTH=$(date +%m)
case "$CURRENT_MONTH" in
01|03|05|07|08|10|12)
PREV_LAST=31;;
04|06|09|11)
PREV_LAST=30;;
02)
CURRENT_YEAR=$(date +%Y)
if [ $(( ${CURRENT_YEAR} % 4)) = 0 ]
then
PREV_LAST=29
else
PREV_LAST=28
fi;;
esac
echo $PREV_LAST
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-06-2006 11:30 PM
тАО11-06-2006 11:30 PM
Re: last day of previous month
cal 10||egrep -v '^$|[a-zA-Z]'|head -1|awk '{print $1}'
cal 10|egrep -v '^$|[a-zA-Z]'|tail -1|awk '{print $NF}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2006 12:12 AM
тАО11-07-2006 12:12 AM
Re: last day of previous month
Actually, there's a Perl one-liner that will accomodate you.
# perl -MDate::Calc=:all -le '$m=shift;$y=shift;print Days_in_Month($y,$m==1 ? 12:$m-1)'
...that is, pass the year and the current month for which you want the last day of the previous month:
# perl -MDate::Calc=:all -le '$m=shift;$y=shift;print Days_in_Month($y,$m==1 ? 12:$m-1)' 3 2004
...returns:
29
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2006 01:22 AM
тАО11-07-2006 01:22 AM
Re: last day of previous month
--------------------------
BEGIN {
t = systime()
m = strftime("%m", t)
this_month = m
while (m == this_month) {
t = t - 86400
m = strftime("%m", t)
}
last_month = m
print "Last day last month = " strftime("%D", t)
while (m == last_month) {
t = t - 86400
m = strftime("%m", t)
}
t = t + 86400
print "First day last month = " strftime("%D", t)
}