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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can use "date" command show date of yesterday?

Status
Not open for further replies.

sukchai

IS-IT--Management
Dec 12, 2002
3
0
0
SG
How can use "date" command show date of yesterday?

I can't find any option of this command to do that. I can minus today's date by 1. Anyway, a problem will happen on a joint of each month.
 
Hi,
Run a cron job every night before midnight and echo the date to a file. The next day you can use the file for comparison purposes.
 
Hi:

First, I don't think you can solve this problem with just the date command unless you take the first suggestion. Second, I'd use perl to solve this problem. If you don't have perl, but you have access to GNU's awk, gawk, the following script provides yesterday's
date:

time.sh -1

You'll have to change the gawk internal strftime function formatting to fit your own fromatting needs.

Go to http// for a copy of gawk.

#!/bin/bash
# time.sh
# Author: Julian Cates
#
# Figure what a date was/will be, given an offset from current day:
#./time.sh -7
# Thu May 17 10:49:06 CDT 2001
#======================================================================
ME=`basename ${0}`
if [ $# -ne 1 ]
then echo "Usage is $ME [+/-] # of days"
else
gawk -v days=$1 'BEGIN {
target_date = systime()+(86400*days)
datestring = strftime("%c", target_date)
printf "%s\n\n",datestring }'
fi

Third, if you don't want to use the GNU tools, then you need to do some programming. I choose to manipulate the Julian Date. Not the modern julian date which counts the number of days from January 1 of the current year, but the astronomer's algorithm that tracks days from Jan. 1, 4713 BC.

There are well known algorithms that are easily implemented. The following ksh script gets the Julian Date of the system date, subtracts 1 to get yesterday's JD, and
converts yesterday's JD to the gregorian date:

#!/bin/ksh

# The Julian date (JD) is a continuous count of days from 1 January 4713 BC. The following
# algorithm is good from years 1801 to 2099
# See URL: for more information
get_JD ()
{
typeset -i JDD

JDD=$(($1-32075+1461*($3+4800+($2-14)/12)/4+367*($2-2-($2-14)/12*12)/12-3*(($3+4900+($2-14)/12)/100)/4))
echo $JDD
}

# This function computes the gregorian date from the julian date - $1. Returns a
# date strin of the form: MONTH DAY YEAR
# See URL: for more information
get_greg_from_JD ()
{
typeset -i L
typeset -i N
typeset -i I
typeset -i J
typeset -i DAY
typeset -i MON
typeset -i YR

L=$(($1+68569)) # $1 is the julian date
N=$((4*L/146097))
L=$((L-(146097*N+3)/4))
I=$((4000*(L+1)/1461001))
L=$((L-1461*I/4+31))
J=$((80*L/2447))
DAY=$((L-2447*J/80))
L=$((J/11))
MON=$((J+2-12*L))
YR=$((100*(N-49)+I+L))

echo $MON $DAY $YR
}


TY="20"$(date '+%y') # Year for today
TM=$(date '+%m') # Month for today
TD=$(date '+%d') # Day for today
JD=$(get_JD TD TM TY) # today's Julian date

YJD=$((JD-1)) # yesterday's julian date
date_str=$(get_greg_from_JD YJD)
# parse yesterday's date string
set - $(echo $date_str)
echo $1 # month
echo $2 # day
echo $3 # year
 
Have a look at this FAQ faq80-953 the first solution is in Perl - so skip that and look at bottom of FAQ for the (very short) shell solution. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Thank you very much, Mrregan, olded, Mikelacey.
 
Here is how I do it in Linux RedHat 6.2

yesterday (0-6); date --date="-1 day" +"%w"
tomorrow (0-6); date --date="+1 day" +"%w"
yesterday (00-31); date --date="-1 day" +"%d"
tomorrow (00-31); date --date="+1 day" +"%d"
yesterday (000-366); date --date="-1 day" +"%j"
tomorrow (000-366); date --date="+1 day" +"%j"
next month (01-12); date --date="+1 month" +"%m"
last month (01-12); date --date="-1 month" +"%m"
next year (00-99); date --date="+1 year" +"%y"
last year (00-99); date --date="-1 year" +"%y"

"week", "year", "minute", "second" also work.

John G. Lynn
 
MikeLacey

I believe someone pointed out that the shell solution in your FAQ80-953 only works in the GMT timezone. Tek-tips search is down at the moment so I couldn't search for it. CaKiwi
 
I got this script from some site and am using it. Hope this helps.
----------------------------
TODAY=`date "+%y %m %d" | nawk '{split("31,31,28,31,30,31,30,31,31,30,31,30",month,",");
if ($1 % 4 == 0)
{
month[3]=29;
};
day=$3-1;
mon=$2+0;
year=$1+0;
if (day == 0)
{
day=month[$2-0];
mon=$2-1;
if (mon == 0)
{
mon=12;
year=$1-1;
}
}
printf("%02d/%02d/%02d",mon,day, year);}'`
echo $TODAY
-----------------------------
 
Hi:

I ran your awk script on my solaris 7 box with no problems. Some versions of awk/nawk can be tricky. I'd try some white space to see if that helps:

# all one line
nawk ' { split ("31,31,28,31,30,31,30,31,31,30,31,30",month,",");

and

printf("%02d/%02d/%02d",mon,day, year);}
'`

Also, awk isn't "C". The semi-colons are totally unnecessary. That could be your problem.

Not that it probably matters, but your leap year function is incorrect. Leap year is a year evenly divisible by 4, provided the year is not divisible by 100, except when divisible by 400. Thus, 2000 is a leap year whereas 1900 is not.

Regards,

Ed
 
Ed, Thanks for the Leap year corrections. I try to include those in my script. As i said I dint write the script myself and am not too good at script. Shall try to work without those semicolns. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top