[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...determine yesterday's date in Unix environment? I...

<< Back to: comp.sys.hp.hpux FAQ

Question by Jim
Submitted on 1/2/2004
Related FAQ: comp.sys.hp.hpux FAQ
Rating: Rate this question: Vote
How can i determine yesterday's date in Unix environment? I have Perl script that can do this but looking for one that can do this function in ksh.


Answer by ComputerGuy
Submitted on 1/19/2004
Rating:  Rate this answer: Vote
# File-name: yesterday.sh
#-----------------------------------------------
# Returns date 1 day ago from the specified date
# Current date is taken if no date is specified
#-----------------------------------------------
# Input:        Default:
#   $1 - dd             Current day
#   $2 - mm             Current month
#   $3 - yyyy           Current year
#-----------------------------------------------

#This is how a function is defined in a
#UNIX shell scripts

get_one_day_before_specified_date()
{
#get the command line input(date month & year)
    day=$1
    month=$2
    year=$3

    # if it is the first day of the month
    if [ $day -eq 01 ]
    then
        # if it is the first month of the year
        if [ $month -eq 01 ]
        then
            # make the month as 12
            month=12

            # deduct the year by one
            year=`expr $year - 1`
        else
            # deduct the month by one
            month=`expr $month - 1`
        fi
        
        # use cal command, discard blank lines,
        # take last field of last line,  
        # first awk  command is used to get the
        # last useful line of the calendar cmd,
        # second awk command is used to get the  
        # last field of this last useful line,
        # NF is no. of fields,
        # $NF is value of last field

day=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' |  awk '{ print $NF }'`
    else
        # deduct the day by one
        day=`expr $day - 1`
    fi

    echo $day $month $year
}

#!/bin/ksh
if [ $# -ne 3 ]
then
    d=`date +%d`
    m=`date +%m`
    y=`date +%Y`
else
    d=$1
    m=$2
    y=$3
fi    

#Cmd line arguments are captured in a shell script
#through $1 $2 $3, ......., $9,${10} (not $10)

# This is how we call unix user-defined functions,
# notice it is not junk123( $1, $2, $3 ) format
get_one_day_before_specified_date $d $m $y

 

Answer by shellmastergeneral
Submitted on 3/26/2004
Rating:  Rate this answer: Vote
Linux (well, GNU date) only:

date -d "1 day ago"

 

Answer by shellmastergeneral
Submitted on 3/26/2004
Rating:  Rate this answer: Vote
BSD:

YESTERDAY=`expr \`date +%s\` - 86400`
date -r $YESTERDAY

 

Answer by ctsgnb
Submitted on 4/14/2004
Rating:  Rate this answer: Vote
Almost all Unix :

TZ=GMT+24 date

(a Great Thanks to Patrice Allais for this handy tip !!!!  )

 

Answer by d. pithon
Submitted on 5/17/2004
Rating:  Rate this answer: Vote
GNU date is your (fantastic) friend

$ date
Mon May 17 15:12:45 CEST 2004

$ date -d yesterday
Sun May 16 15:13:03 CEST 2004

$ date -d "Oct 10 1984 2 day ago"
Mon Oct  8 00:00:00 CET 1984

$ date -d tomorrow
Tue May 18 15:13:32 CEST 2004

$ date -d "2006-10-26 18:35:00 2 day ago"
Tue Oct 24 18:35:00 CEST 2006

$ date -d "2006-10-26 18:35:00 2 day ago 3 hours"
Tue Oct 24 21:35:00 CEST 2006

$ date -d "2006-10-26 18:35:00 2 day ago 3 hours ago"
Sat Oct 28 15:35:00 CEST 2006

 

Answer by 8isgr8
Submitted on 3/17/2005
Rating: Not yet rated Rate this answer: Vote
You can also apply to formating

date +%y%m%d -d"1 day ago"

This returns

050316

I use this when I copy yesterdays log files and I want to keep the formating.

YDAY=`date +%y%m%d -d"1 day ago"`

I then zip it up for a windows friend ;)

zip -r log"$YDAY" . -i \*.log


 

Answer by D.DASQUE
Submitted on 4/1/2005
Rating: Not yet rated Rate this answer: Vote
this command

TZ=GMT+24 date

must be adapt to your time zone
i.e. (Your time zone )+24
example for France GMT+22


 

Answer by Splitz
Submitted on 5/12/2005
Rating: Not yet rated Rate this answer: Vote
FreeBSD....

21 days ago = date -v-21d
1 month and 7 days ago = date -v-7d -v-1m

 

Answer by Mohit Garg
Submitted on 10/11/2005
Rating: Not yet rated Rate this answer: Vote
Try this. It works well
dd=`date +%d`
mm=`date +%m`
YY=`date +%Y`
HH=`date +%H`
MM=`date +%M`
SS=`date +%S`

let "today=(dd*86400)+(HH*3600)+(MM*60)+SS"
let "yesterday=today-86400"

let "y_YY=YY"
let "y_mm=mm"
let "y_dd=yesterday/86400"
let "y_HH=(yesterday%86400)/3600"
let "y_MM=((yesterday%86400)%3600)/60"
let "y_SS=((yesterday%86400)%3600)%60"

if (( dd==1 ))
then    let y_mm=mm-1
   case $y_mm in
      1|3|5|7|8|10|12   )    let y_dd=31 ;;
      4|6|9|11 )      let y_dd=30 ;;
      2)   if (( YY%4 == 0 ))
            then let y_dd=29
            else let y_dd=28
         fi
         ;;
   esac   
fi
if (( mm==1 ))
then   let y_YY=YY-1
   let y_mm=12
   let y_dd=31

fi
typeset -Z2  mm dd HH MM SS y_mm y_dd y_HH y_MM y_SS
echo "Today     : $YY/$mm/$dd $HH:$MM:$SS"
echo "Yesterday : $y_YY/$y_mm/$y_dd $y_HH:$y_MM:$y_SS"

 

Answer by ulum
Submitted on 5/5/2006
Rating: Not yet rated Rate this answer: Vote
that's function is not valid.
cause when we on, let's say 1 may 2006. on that date yesterday is 30 apr 2006 but its showing 0 may 2006

 

Answer by nyamijal
Submitted on 7/27/2006
Rating: Not yet rated Rate this answer: Vote
The following c prog should give the necessary output needed.

#include <stdio.h>
#include <time.h>
int main() {

        time_t currenttime;
        time(¤ttime);
        currenttime = currenttime - 86400;
        printf("%s\n",ctime(¤ttime));
        return 1;
}

compile it as 'cc yest_time.c -o yesterday' or with any of your faviourite compiler. then,
# ./yesterday
Wed Jul 26 11:35:45 2006
# date
Thu Jul 27 11:37:00 IST 2006

 

Answer by wwiggens
Submitted on 10/3/2006
Rating: Not yet rated Rate this answer: Vote
Linux:
yest
(sourceforge.net/projects/yest)

 

Answer by Gabbar
Submitted on 11/21/2006
Rating: Not yet rated Rate this answer: Vote
#! /usr/bin/ksh

# Today's date
TODAY=`date +'%m-%d-%Y'`

# Setting month year & date
MONTH=`echo $TODAY | cut -d'-' -f1`
DAY=`echo $TODAY | cut -d'-' -f2`
YEAR=`echo $TODAY | cut -d'-' -f3`

if test $DAY -eq 1
then
   if test $MONTH -eq 1
     then
        YEAR=`expr $YEAR - 1`
   MONTH=12
   DAY=31
   echo $MONTH/$DAY/$YEAR
   exit
   fi

   if test $MONTH -eq 3
     then
        MONTH=`expr $MONTH - 1`
        cal $MONTH $YEAR | tail +2 | grep -q 29
   if test $? -eq 0
   then
      DAY=29
   else
      DAY=28
        fi

     else
        MONTH=`expr $MONTH - 1`
        cal $MONTH $YEAR | tail +2 | grep -q 31
   if test $? -eq 0
   then
     DAY=31
        else
     DAY=30
        fi
     fi


else
    DAY=`expr $DAY - 1`
fi
echo "$MONTH/$DAY/$YEAR"

 

Answer by MATA
Submitted on 11/23/2006
Rating: Not yet rated Rate this answer: Vote
GREAT HELP

 

Answer by dorg
Submitted on 3/2/2007
Rating: Not yet rated Rate this answer: Vote
To get month of yesterday
GNU DATE
YYYY-MM of yesterday
date +"%Y-%m" -d yesterday

 

Answer by remo
Submitted on 3/22/2007
Rating: Not yet rated Rate this answer: Vote
DATE_STAMP=`TZ=CST+24 date +%y%m%d`

http://www.unix.com/showthread.php?s=&threadid=13731

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.sys.hp.hpux FAQ


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.