The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare date from db2 table to yesterday's Unix system date sasaliasim Shell Programming and Scripting 8 04-24-2008 03:04 AM
Date Function charandevu Shell Programming and Scripting 1 04-02-2008 04:44 AM
How can i get the yesterday's date in YYYYMMDD format prasadsr HP-UX 4 01-19-2007 05:52 AM
get yesterday's date? fedora Shell Programming and Scripting 1 12-08-2006 11:28 AM
Yesterday's date ssmiths001 UNIX for Dummies Questions & Answers 2 11-22-2004 05:46 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2005
Registered User
 

Join Date: Nov 2004
Posts: 41
Stumble this Post!
Yesterday's date function

I am using this function to calculate yesterday's date and return it in the following format: Jan 09

date '+%b %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
((DAY < 10)) && DAY="0"$DAY
}

On the last day of December, it returned 31 and I wanted it to return Dec 31. Can someone tell me what could be wrong? After I return the value, I then am using it to grep a logfile, looking for yesterday's date.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-10-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,448
Stumble this Post!
That code is garbage. There must be some typos, but even allowing for that, "expr "Jan - 1"? You gotta be kidding. Navigate: our home page -> Answers to Frequently Asked Questions -> Yesterdays Date/Date Arithmetic and look for my datecalc routine. Then:

typeset -Z2 d
set -A months XXX Jan Feb Mar Apr May (etc)
datecalc -a $(date '+%Y %m %d') - 1 | read y m d
echo "${months[m]} $d"
Reply With Quote
  #3 (permalink)  
Old 01-10-2005
Registered User
 

Join Date: Nov 2004
Posts: 41
Stumble this Post!
I've put your function in a directory called functions. In the calling program I have the following:

. /app/manuv711/dnetwork/sfi/functions/datecalc

typeset -Z2 d
set -A months XXX Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
datecalc -a $(date '+%Y %m %d') - 1 | read y m d
echo "${months[m]} $d"


When I execute it all I get it this:

datecalc -a year month day - year month day
datecalc -a year month day [-|+] n
datecalc -d year month day
datecalc -D year month day
datecalc -j year month day
datecalc -j n
datecalc -l year month
use "datecalc -help" use for more documentation


Am I doing something wrong?
Reply With Quote
  #4 (permalink)  
Old 01-10-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,448
Stumble this Post!
Lose that first line. Don't use the . command like that. Use the 4 lines that I posted. They assume that datecalc is on your PATH. If that's not the case, modify the third line to be a full path to datecalc.
Reply With Quote
  #5 (permalink)  
Old 01-11-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Stumble this Post!
Just for information, if you're using GNU date, you can do some *very* cool stuff with the "-d" option like

$ # get yesterdays date and format the output
$ date -d "yesterday" +"%d %b %Y"
10 Jan 2005

$ # get the date in two mondays time
$ date -d "next 2 monday"
Mon Jan 17 00:00:00 GMTST 2005

$ # 3 weeks ago...
$ date -d "3 weeks ago"
Tue Dec 21 14:06:44 GMTST 2004

You get the idea.

Cheers
ZB
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0