How to get the date yesterday in AIX sh | Unix Linux Forums | AIX

  Go Back    


AIX AIX is IBM's industry-leading UNIX operating system that meets the demands of applications that businesses rely upon in today's marketplace.

How to get the date yesterday in AIX sh

AIX


Tags
aix, unix

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-03-2008
victorcheung victorcheung is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 17 June 2012, 11:20 PM EDT
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
How to get the date yesterday in AIX sh

Hi,

In AIX sh, how to return the date of yesterday in format of %Y%m%d, YYYYMMDD.

i.e. if today is 20080704, I want it return 20080703.

Can anyone help?

Thanks!
Victor Cheung
Sponsored Links
    #2  
Old 07-03-2008
victorcheung victorcheung is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 17 June 2012, 11:20 PM EDT
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
I have found it from this forums:
YESTERDAY=`TZ=aaa24 date +%Y%m%d`

Thanks!
Sponsored Links
    #3  
Old 07-06-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 9 April 2014, 1:31 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts
Lightbulb

date command internally refers to the env variable TZ(time zone)
so just change TZ=IST+24
then execute date command in whichever format you get yesterdays date
then revert back the TZ variable to TZ=IST-5:30
(but this changes are temporary)
    #4  
Old 03-10-2009
kdevries kdevries is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 10 March 2009, 6:30 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
This was THE solution I have been looking for so long !!, GREAT
Thanks again, saved my day
Sponsored Links
    #5  
Old 03-31-2009
kah00na's Avatar
kah00na kah00na is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 15 October 2014, 11:26 AM EDT
Location: Kansas
Posts: 298
Thanks: 21
Thanked 24 Times in 21 Posts
I got sick of trying to script yesterday's date into so many different formats that I finally wrote and stole portions of this script to do it for me. I just pass in any arguments I want and out comes yesterdays date. I named the script "yesterday" and put it in /usr/local/bin on all my boxes:


Code:
FORMAT=${1:-YYYYMMDD}

if [ "$FORMAT" = "-help" ] || [ "$FORMAT" = "-h" ]; then
        echo "Usage:    Arguments are optional and can be given in any order"
        echo "          yesterday  [YY|yy|YYYY|yyyy|MM|mm|MMM|mmm|DD|dd]"
        exit 0
fi

typeset -Z2 YESTERDAY
typeset -Z2 MONTH

YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%Y`
if [ "$YESTERDAY" -eq "0" ]; then
        MONTH=$((MONTH-1))
        if [ $MONTH -eq "0" ]; then
                MONTH=12
                YEAR=$((YEAR-1))
        fi
        set `cal $MONTH $YEAR`
        shift $(($# - 1))
        YESTERDAY=$1
fi

ABBR_MONTH=$(echo $MONTH | sed \
        -e "s/01/Jan/" \
        -e "s/02/Feb/" \
        -e "s/03/Mar/" \
        -e "s/04/Apr/" \
        -e "s/05/May/" \
        -e "s/06/Jun/" \
        -e "s/07/Jul/" \
        -e "s/08/Aug/" \
        -e "s/09/Sep/" \
        -e "s/10/Oct/" \
        -e "s/11/Nov/" \
        -e "s/12/Dec/")

## Print the result
echo "${FORMAT}" | sed \
        -e "s/[Mm][Mm][Mm]/${ABBR_MONTH}/" \
        -e "s/[Yy][Yy][Yy][Yy]/${YEAR}/" \
        -e "s/[Yy][Yy]/$(echo ${YEAR}|cut -c3-4)/" \
        -e "s/[Mm][Mm]/${MONTH}/" \
        -e "s/[Dd][Dd]/${YESTERDAY}/"

Here is some examples of it running:

Code:
hostname:/:$ date
Tue Mar 31 16:28:15 CDT 2009
hostname:/:$ yesterday yyyymmdd
20090330
hostname:/:$ yesterday yyyy/mm/dd
2009/03/30
hostname:/:$ yesterday MM/DD/YYYY
03/30/2009
hostname:/:$ yesterday MM-DD-YYYY
03-30-2009
hostname:/:$ yesterday "MM dd YYYY"
03 30 2009
hostname:/:$

The Following User Says Thank You to kah00na For This Useful Post:
EW1(SG_ (02-23-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help in Shell Script comparing todays date with Yesterday date from Sysdate kumarmsk1331 Shell Programming and Scripting 0 07-29-2011 04:49 PM
yesterday date thelakbe Shell Programming and Scripting 7 01-17-2011 12:26 PM
Getting yesterday `date` ahamed Shell Programming and Scripting 1 01-25-2010 09:15 AM
Compare date from db2 table to yesterday's Unix system date sasaliasim Shell Programming and Scripting 9 12-01-2008 10:37 PM
yesterday date month/date skully Shell Programming and Scripting 5 06-24-2008 05:51 AM



All times are GMT -4. The time now is 02:13 PM.