- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to get (date -1) for 1st day of every 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
Forums
Discussions
Discussions
Discussions
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
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
01-11-2009 07:12 PM
01-11-2009 07:12 PM
How to get (date -1) for 1st day of every month
I would like to know how to get date-1 for the 1st day of every month. For e.g,on 1st Feb, i want to get 20090131,on 1st Jan i want to get 20081231.
FYI, I am trying to write a script for that. Kindly give the necessary details.
Regards
Feng Lin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2009 07:25 PM
01-11-2009 07:25 PM
Re: How to get (date -1) for 1st day of every month
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=541033
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=213185
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2009 08:51 PM
01-11-2009 08:51 PM
Re: How to get (date -1) for 1st day of every month
MON=12
YR=2008
print $(cal $MON $YR) | awk '{print $NF}'
Just compute month - 1 and roll the year if month - 1 is less than 1. Handles all the dates in the past from year 1 to year 9999 (Gregorian). It works by stringing the cal output onto one line. Therefore, the last number on the line will always be the last day of the month:
print $(cal 1 2008)
January 2008 S M Tu W Th F S 1 2 ... 28 29 30 31
awk '{print $NF}' always returns the last space-delimited string on a line.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2009 02:41 AM
01-12-2009 02:41 AM
Re: How to get (date -1) for 1st day of every month
Yet another way is to use Perl. In your shell script, do something like:
...
if [ "$(date '+%d') = 1 ]; then # First of month
YESTERDAY=$(perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time-86400)')
echo ${YESTERDAY}
fi
You can use the same formatting directives you use with the Unix 'date' command with the 'strftime' function in the Perl script.
The computations are done in Epoch seconds, so the value of 86400 seconds is one-day.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2009 02:53 AM
01-12-2009 02:53 AM
Re: How to get (date -1) for 1st day of every month
Instead of using large magic numbers, you might want to use smaller identifiable ones: 24*60*60
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2009 12:01 PM
01-12-2009 12:01 PM
Re: How to get (date -1) for 1st day of every month
> Dennis: Instead of using large magic numbers, you might want to use smaller identifiable ones: 24*60*60
And sometimes I do just that. You can see that my commentary noted, "The computations are done in Epoch seconds, so the value of 86400 seconds is one-day."
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2009 03:24 PM
01-12-2009 03:24 PM
Re: How to get (date -1) for 1st day of every month
Yes. But if you use 24*60*60 you may not have to have that comment. :-)