Yesterday's date function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Yesterday's date function
# 1  
Old 01-10-2005
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  
Old 01-10-2005
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  
Old 01-10-2005
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  
Old 01-10-2005
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  
Old 01-11-2005
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
This User Gave Thanks to zazzybob For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

2. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

3. Shell Programming and Scripting

How to get tomorrow,yesterday date from date Command

Hi I want to get tomorrow and yesterday date from date command. My shell is KSH and server is AIX. I tried several options, but unable to do. Please help on this. Regards Rajesh (5 Replies)
Discussion started by: rajeshmepco
5 Replies

4. Shell Programming and Scripting

Get yesterday date

Hi Friend, i am using OS HP-UX vvftf320 B.11.11 U 9000/800 511076331 unlimited-user license now i have used below command but it giving today's date. i need your help to get yesterdate. Please correct me. date +"%d%m%Y%H%M%S" -d "1 days ago Thanks in advance, Jewel (3 Replies)
Discussion started by: Jewel
3 Replies

5. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

6. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

7. Shell Programming and Scripting

Getting yesterday `date`

Hi, `date` command will give the current days date. Is there any command to get the previous day date? I need the previous day value in my script. Ahamed. (1 Reply)
Discussion started by: ahamed
1 Replies

8. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies

9. Shell Programming and Scripting

yesterday date month/date

Hi expert, I want to retrieve yesterday su log. How to calculate and assign variable value ( 06/23 ) in myVariable ? #!/bin/sh myVariable=yesterday date in month/date cat /var/adm/sulog | grep $myVariable > file.txt many thanks! (5 Replies)
Discussion started by: skully
5 Replies

10. Shell Programming and Scripting

get yesterday's date?

Hello, using date, we can easily get today's date $ date +%y-%m-%d 06-12-08 is it possible for me to get yesterday's date using 'date', if not, is there any quick and easy way to do that? Thanks! (1 Reply)
Discussion started by: fedora
1 Replies
Login or Register to Ask a Question