date - 1 day


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers date - 1 day
# 1  
Old 11-20-2008
Question date - 1 day

Hi, I have been trying just about every unix command to come up with yesterday's date (today's date - 1). I have seen all of the help on this forum, and none of it seems to work for me here. We are using Sun Solaris 9 Unix. I am using this script to create a .txt file with ftp commands that I will run from another script. All I want to do, is retrieve whatever files are at the ftp site with a date of yesterday in the filename. I have been trying to get the date right with Unix but obviously I am not having much luck.


I have tried a number of ways to get yesterday's date and none work for me.

#CalcDate=`date +%Y%m%d`
#CalcDate=`date -d yesterday +%Y%m%d`
#date="1 day ago" +"%Y%d%m"

I am able to output today's date to the .txt file using the following (see below).

CalcDate=`date +%Y%m%d`
cd /apps/PT8.44/outputs/backflow
echo 'lcd /apps/PT8.44/outputs/backflow/test' > bflowin.txt
echo 'mget *'$CalcDate* >> bflowin.txt
echo 'quit' >> bflowin.txt

Contents of bflowin.txt after script runs: I need it to be 20081119 (yesterday's date).

lcd /apps/PT8.44/outputs/backflow/test
mget *20081120*
quit

Any help you guys can give would be appreciated.
# 2  
Old 11-20-2008
This is how I had to get yesterdays day (HP-UX):
Code:
OFFSET=${1:-1}

case $OFFSET in
  *[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year

day=$((day - OFFSET))
if (( day <= 0 )) ;then
  month=$((month - 1))
  if (( month == 0 )) ;then
    year=$((year - 1))
    month=12
  fi
  set -A days `cal $month $year`
  xday=${days[$(( ${#days[*]}-1 ))]}
  day=$((xday + day))
fi

echo $day

or try:

Code:
date '+%m %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  
echo "Yesterday was: $MONTH $DAY $YEAR"  
}

# 3  
Old 11-21-2008
Thanks Ikon......the second code example worked perfectly for me. I am no longer frustrated! Have a great weekend :-)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. Shell Programming and Scripting

Need to minus one day from a date given

Hello Folks, I have a variable output holding date as below - output = "20141220" I need to extract a day out of it and store it in another variable i.e. something similar to below - output1=20141219" and if the month is changing i.e. date in on 31st or 1st it should be taken care of "date... (5 Replies)
Discussion started by: ektubbe
5 Replies

3. Shell Programming and Scripting

Date minus 1 day

Hi the below code is failing i am trying to generate do the following: 2014-10-22 11:26:00 (Substract 24 hrs) should produce 2014-10-21 11:26:00 I need the same formatting below because that gets inputted into code for an... (4 Replies)
Discussion started by: Samuel12
4 Replies

4. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

5. Shell Programming and Scripting

Date to Day in loop

Hi All, I have a file in the following format.I need to pick up 25th field which is a date and convert it into a day and add it as a field in the file. "AAGENAS,PEARL... (3 Replies)
Discussion started by: nua7
3 Replies

6. Shell Programming and Scripting

Day before yesterday's date

Hello All, I am writing a script in Sun Solaris I want the date for "day before yesterday", i got the yesterday's date by this command TZ=GMT+24 date +%b" "%d. Please suggest me some code to get the date for day before yesterday (6 Replies)
Discussion started by: anand2308
6 Replies

7. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

8. UNIX for Dummies Questions & Answers

Getting date -1 day not using GNU date

It's easy as pie to get the date minus one day on opensolaris: date -d "-1 day" +"%Y%m%d"run this command on our crappy Solaris 10 machines however (which I'm guessing doesn't have GNU date running on it) and you get: date: illegal option -- d date: illegal option -- 1 date: illegal option --... (5 Replies)
Discussion started by: rich@ardz
5 Replies

9. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

10. Shell Programming and Scripting

Getting day from a date...

Hi, I have a date input in MMDDYYYY format.. I have to give the day (whether that DD is sunday/monday...) Is there any command for it... Or do I have to write a script for that... Thanks in Advance Yeheya (1 Reply)
Discussion started by: yeheyaansari
1 Replies
Login or Register to Ask a Question