- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Formatting a date
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
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
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
тАО06-23-2008 06:22 AM
тАО06-23-2008 06:22 AM
Problem is, I can't find any function that'll format a date other than using /usr/bin/date to format the current date/time. I guess I can use perl, but I'm trying to make the script entirely ksh for portability on a variety of OS versions (perl versions in our environment vary and are in different locations on some boxes).
Do I have any options other than writing a staggeringly lengthy script, or using a command-line perl call? I feel like I knew a command to do this, but I can't come up with it off the top of my head and google and man have failed me.
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 06:24 AM
тАО06-23-2008 06:24 AM
Re: Formatting a date
Without knowing how you want it formatted, it's kind of difficult to make any recommendations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 06:33 AM
тАО06-23-2008 06:33 AM
Re: Formatting a date
date +%M%D%Y, date +%m%d%y, date +%m-%d-%Y
just about anything is possible.
Did you try to simply read the man pages ? It is a great local source for information on unix commands.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 06:57 AM
тАО06-23-2008 06:57 AM
Re: Formatting a date
./security_info.ksh 06/20
or
./security_info.ksh 20080620
and have it send out an email with a subject line of:
"Security Info for Friday, June 20th, 2008"
The script is set up to do that if you just type "./security_info.ksh" (it defaults to the current date and formats accordingly), but on occasion I need to generate an email for a date in the past.
Hope this is clearer. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 07:10 AM
тАО06-23-2008 07:10 AM
Re: Formatting a date
Dates in the past can easily be generated by Perl:
# perl -le 'print scalar localtime(time()-(60*60*24))'
Sun Jun 22 11:09:46 2008
...yields exactly yesterday in your localtime.
Now, use the POSIX module to format the date just like you would use the Unix 'date' command:
# perl -MPOSIX -le 'print strftime "%m/%d/%Y %H:%M:%S",localtime(time()-(60*60*24))'
06/22/2008 11:09:48
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 09:17 AM
тАО06-23-2008 09:17 AM
Re: Formatting a date
The perl option is the simplest.
There are others that us a short C program that converts the date to epoch, does the math, then converts back to human.
There is another that is all ksh and breaks out day, month, year, puts into arrays and allows for add/sub that way but is would be something you would wish to call as a sub script.
Do some searching and pick the one you like best.
Otherwise you will have to write something yourself and will run into the same issue as others, what do I do when I reach the EOM, or leap year, or begining of month....What if I want the last work day of the month....
Best of luck..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 09:18 AM
тАО06-23-2008 09:18 AM
Re: Formatting a date
"Security Info for Friday, June 20th, 2008"
It may be better to not format it. Then it will sort by date for the YYYYMMDD format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 10:23 AM
тАО06-23-2008 10:23 AM
Re: Formatting a date
perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(mktime(0,0,0,21,6,2008))'
Which I would assume should return 6/21/2008, but instead returns:
12/31/1969
So somehow it seems like mktime is returning 0. I'm working on it, but if you can spot any obvious idiocy on my part it would be appreciated. :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 10:40 AM
тАО06-23-2008 10:40 AM
SolutionIn you last post:
# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(mktime(0,0,0,21,6,2008))'
...this should be:
# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(mktime(0,0,0,21,5,108))'
...assuming that you were trying to show June 21, 2008. The month is zero-relative. The year is the year since 1900.
Notice that you don't call 'mktime()' as it is implied.
If I'm calculating some time in the past or future, I always use the epoch time +- some number of seconds as I first showed.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 10:52 AM
тАО06-23-2008 10:52 AM
Re: Formatting a date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-23-2008 11:42 AM
тАО06-23-2008 11:42 AM
Re: Formatting a date
When I said that you don't need to invoke 'mktime()' I *meant* to write:
# perl -MPOSIX -le 'print strftime "%m/%d/%Y",0,0,0,21,5,108'
...thereby simplifying your requirement.
Regards!
...JRF...