![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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? |
|
||||
|
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 |
||||
| Google The UNIX and Linux Forums |