- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Date format
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
07-26-2009 06:18 PM
07-26-2009 06:18 PM
MTHLESS="$((`date +%m`-1))"
using the above command,I am able to get the current month -1, I would like to know whether it will work when it comes to January. Will I be able to get December as the output?
Please advice.
Regards
Feng Lin
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 01:02 AM
07-27-2009 01:02 AM
Re: Date format
01 - 1 = 0
Which I suspect is not what you want...
Why not install the GNU version of date which is available in the GNU coreutils package available here:
http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-7.4/
(Don't miss the runtime dependencies you need too)
Once you have that you can easily do things like this:
# /usr/local/coreutils/bin/date +%m
07
# /usr/local/coreutils/bin/date -d "last month" +%m
06
HTH
Duncan
I am an HPE Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 02:16 AM
07-27-2009 02:16 AM
Re: Date format
You can get 12 if you do:
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 03:51 AM
07-27-2009 03:51 AM
Re: Date format
If the month is all that you want, you can do:
# awk -v $(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 08:27 AM
07-27-2009 08:27 AM
Re: Date format
I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?
Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 08:38 AM
07-27-2009 08:38 AM
Re: Date format
> Dennis: I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?
I never said that is was. I'm merely pointing out TIMTOWDI ;-)
> Dennis: Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.
The BEGIN rule takes care of that _if_ I hadn't bungled the variable assignment:
# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 05:24 PM
07-27-2009 05:24 PM
Re: Date format
Thanks for the info.
Now I have the following situation : -
I have files with naming convention *yyyymmdd*. How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?
I intend to run the housekeeping job twice a month on the 1st and 16th of the month. 1st is to archive 16 - 31st day files, 16th is to archive 1 - 15th day files.
Thanks a lot.
Regards
Feng Lin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 05:39 PM
07-27-2009 05:39 PM
Re: Date format
> How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?
# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}'
06
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2009 06:03 PM
07-27-2009 06:03 PM
SolutionI guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN.
I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.
>How am I going to take care of the zero (fill) (Jan - Sep) and one(Oct - Dec) for the month
typeset -Z2 MTHLESS # zero fill
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
echo $MTHLESS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2009 04:18 AM
07-28-2009 04:18 AM
Re: Date format
> Dennis: I guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN. I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.
I agree that the input file(s) are not read in the case of a BEGIN block with no pattern-action statements in the subsequent main block. This is easy to demonstrate.
Of course, this behavior doesn't occur if an END block is declared either alone or in concert with a BEGIN block. In that case, at least one file must be input.
I concede that relying on these subtitles may lead to confusion and/or false expectations. Hence it is probably best to amend the 'awk' snippet to:
# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null
...and then (similarly) this works too:
# awk -v MON=$(date +%m) 'END{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2009 06:23 PM
07-29-2009 06:23 PM
Re: Date format
I am using ur method,typeset etc.....
I should be getting 06 as the output for the current month,instead I am still getting 07.
Please advice.
Regards
Feng Lin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2009 06:51 PM
07-29-2009 06:51 PM
Re: Date format
Oops, off by one:
(( MTHLESS = ($(date +%m) + (12-1-1)) % 12 + 1 ))
The extra -1 and +1 bias the 0..11 to 1..12. The first 12 is to prevent negative numbers because % is rem and not mod.
And of course the other -1, is the previous month.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2009 11:11 PM
08-03-2009 11:11 PM
Re: Date format
Many thanks for your response. I have one last question.
I have 2 scripts, one running on the 1st, the other running on the 16th.
1st is to zip from the 16th to 31st day of the month and 16th is to zip from the 1st to 15th day of the month.
However,when the script 1st run for the next year, it won't be able to zip from the 16th to 31st day of last month.
For e.g, on 1st Jan 2010, the script won't be able to zip from the 16th dec files to 31st dec files.
How can my script take care of this last issue?
Regards
Feng Lin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2009 12:08 AM
08-04-2009 12:08 AM
Re: Date format
>How can my script take care of this last issue?
How does your script handle the previous month, if in the same year?
I assume you just check if Dec and subtract one from the year.
Have you looked at Clay's caljd script to date arithmetic?
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1232368
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1173224
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1158441
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1101623