Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date manipulation output

Status
Not open for further replies.

bhogaj31

IS-IT--Management
Mar 11, 2003
114
GB
Can anyone help me with this

When I type "date" on a unix machine I get the
"Tue Jul 27 08:51:18 GMT 2004" output.

How can I get the it in the format "20040727", I don't need the time necessarily, but it doesn't matter if I get it.

Also I want to be able to increment/decrement it by whatever number up to 9, I choose but cannot make any sense of the man pages.

Is it smart enough to know when it gets to the end of a month, to increment the month and then start the day from 01 again.

Any help much appreciated.


Regards
 
The man pages for date on Solaris are not as good as the ones on Compaq Tru64 - try Google

To produce the character string in the format yyyymmdd try:
datestring=`date +'%Y%m%d'`

This should work in Bourne, Korn and bash shells. Please note the different use of single quotes.

"datestring" is now a character string and so has no understanding of days, months and years. If you want produce a "datestring" up to 9 days in the past or future, then investigate the timezone variable TZ. (Google can be of great help).

I hope that helps.

Mike
 
I have a script that allows me to select a date from a list. It is useful if you are writing an interactive script and have perl...
Code:
#!/usr/bin/ksh
Days=19
PS3='Choose a date (or q to quit) ? : '
select Date in $(perl -e '
  $d = time() + 86400 * int($ARGV[0] / 2);
  for ($i = 0; $i < $ARGV[0]; $i++) {
    @f = localtime($d);
    printf "%04d%02d%02d\n", $f[5]+1900, $f[4]+1, $f[3];
    $d-=86400;
  }' $Days)
do
  break
done
echo Date is ${Date?Aborted}
It should show a list of dates relative to today like this...
[tt]
1) 20040805
2) 20040804
3) 20040803
4) 20040802
5) 20040801
6) 20040731
7) 20040730
8) 20040729
9) 20040728
10) 20040727
11) 20040726
12) 20040725
13) 20040724
14) 20040723
15) 20040722
16) 20040721
17) 20040720
18) 20040719
19) 20040718
Choose a date (or q to quit) ? : 13
Date is 20040724[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top