The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 9 12-01-2008 11:37 PM
Date Function charandevu Shell Programming and Scripting 1 04-02-2008 07:44 AM
How can i get the yesterday's date in YYYYMMDD format prasadsr HP-UX 4 01-19-2007 09:52 AM
get yesterday's date? fedora Shell Programming and Scripting 1 12-08-2006 03:28 PM
Yesterday's date ssmiths001 UNIX for Dummies Questions & Answers 2 11-22-2004 09:46 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-10-2005
ssmiths001 ssmiths001 is offline
Registered User
  
 

Join Date: Nov 2004
Posts: 41
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.
  #2 (permalink)  
Old 01-10-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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"
  #3 (permalink)  
Old 01-10-2005
ssmiths001 ssmiths001 is offline
Registered User
  
 

Join Date: Nov 2004
Posts: 41
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?
  #4 (permalink)  
Old 01-10-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #5 (permalink)  
Old 01-11-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0