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

HELP USING DATE IN A SCRIPT 2

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I want to use yesterday's date in a script. I.E. If today is 4/1/2003, I want to use 033103.

I was using the example below, but it won't work because it tries to subtract 1 from 1 which results in a 0, but I want 0331.

D=`date +%d`
D=$(($D - 1))
M=`date +%m`
Y=`date +%y`
DATE=$M$D$Y

Does anyone have any ideas as to how I can do this?

Thanks,

John
 
Here you go for getting y'days date.

#!/bin/sh

# ydate: A Bourne shell script that
# prints yestarday's date
# Output Form: Month Day Year

# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`

# Subtract one from the current day.
day=`expr $day - 1`

# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then

# Find the preivous month.
month=`expr $month - 1`

# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`

# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi

# Print the month day and year.
echo $month $day $year
exit 0
 
I didn't think it would have to be that complicated, but it works great!! Thanks new2everything!!

John
 
John:

Here's a julian date solution that I ripped off, er borrowed, from the US Naval Observatory. It works on other dates other than yesterdays:

#!/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=$(date '+%Y') # Year for today
TM=$(date '+%m') # Month for today
TD=$(date '+%d') # Day for today
# printf "%s %s %s\n" $TM $TD $TY
JD=$(get_JD TD TM TY) # today's Julian date

# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD-1)) )

# parse yesterdays date string
set - $(echo $yesterdays_date_str)
echo $1 # month
echo $2 # day
echo $3 # year
# end script
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top