Get the yesterday's date based on sysdate

How to get the yeterday's date base on the sysdate using shell programming
sumanth_oraAsked:
Who is Participating?
 
yuzhCommented:
Here's the testing result from my box:

SunOS mybox 5.8 Generic_117350-47 sun4u sparc SUNW,Ultra-250

 # TodaysDate=`date +%Y%m%d`
# Yesterdaydate=`TZ=$TZ+24 date +%Y%m%d`
# echo "Today: $TodaysDate"
Today: 20071004
# echo "Yesterday: $Yesterdaydate"
Yesterday: 20071003

Could you please type in:
echo $TZ
see what you get?
0
 
yuzhCommented:
for ksh/sh/bash, you can do:

Yesterday=`TZ=$TZ+24 date `
echo $Yesterday

PS: you can reformat the date out put as you wish, eg:
Yesterday=`TZ=$TZ+24 date +%D`
0
 
ghostdog74Commented:
if you have GNU date

date --date='1 day ago'
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
omarfaridCommented:
Hi,

yuzh:

I tried your solution on my server and it is not working. I have solaris 8 and I tried it with ksh & bash.

I am missing any thing?

0
 
ahoffmannCommented:
hmm, Tintin's suggestion works for me with all common shells (ash,bash,csh,ksh,pdsh,tcsh,zsh) on Solaris 8
I.g calculating dates this way with an offset >24 hours depends on the version of date you use. All versions of Gnu's date can do, IIRC. But older versions on BSD-based systems (like Solaris) won't. Do you probably have an old executable.
0
 
yuzhCommented:
It works for me, try to cut and past my comment and test it
see what you get.

any error message?

BTW, ` is back quote, not the ' - single quote!

Good night!
0
 
ahoffmannCommented:
oops, I meant yuzh's suggestion, obviously ...
0
 
omarfaridCommented:
Hi,

Thank you both, but I did copy & paste and I still not able to get previous day date.
0
 
sumanth_oraAuthor Commented:
I need the yesterday date in the following format's

TodaysDate=`date +%Y%m%d`

Yesterdaydate= ??????


TodaysDate=`date +%m%d%y`

Yesterdaydate= ????????


TodaysDatee=`date +%d%m%y`

Yesterdaydate= ???????

TodaysDate=`date +%d_%b_%Y`

Yesterdaydate= ????????
0
 
TintinCommented:
It's not really a problem with older/new versions of Solaris.

On Solaris 10, if I run the following script at 10:30

#!/bin/sh
TodaysDate=`date +%Y%m%d`
Yesterdaydate=`TZ=$TZ+24 date +%Y%m%d`

echo "Today: $TodaysDate"
echo "Yesterday: $Yesterdaydate"


I get

Today: 20071004
Yesterday: 20071002


0
 
omarfaridCommented:
Hi,

Tintin:

I do not know why I am not able to get the same results as you mentioned as well as yuzh and ahoffmann.

What could be wrong  on my system that is causing this to happen? Is there any particular setting I have to do?  (I can make a separate question for this since I have points :) )

0
 
omarfaridCommented:
Hi,

Tintin:

I noticed that your output is 2 days back. Is it a typo?

Thanks,
0
 
TintinCommented:
Definitely not a typo.
0
 
omarfaridCommented:
Hi,

You mean that it is correct to return 2 days back, or it should show one day back?

0
 
TintinCommented:
The TZ trick work most of the time, but it is tripped up by DST changes and the actual timezone you are in.

If you can't install GNU date, then better to use one of the many scripts available to calculate yesterday's date, eg:

#! /usr/bin/ksh
# Get yesterday's date in YYYY-MM-DD format.
# With argument N in range 1..28 gets date N days before.
# Tapani Tarvainen January 2002
# This code is in the public domain.

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

# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
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

print $year$month$day

0
 
TintinCommented:
yuzh, it depends what TZ you are in and more importantly what time of the day you run it.

From memory, you're in Australia.  I'm in NZ and TZ is set to NZ.

If I run it before 12:00, the result will be incorrect.  Remember that NZ is currently GMT-13
0
 
yuzhCommented:
Tintin,
    Good memory! Yes, you are my next door neighbor.
    Have a nice day, cheers!
0
 
omarfaridCommented:
Hi,

I tested the both yuzh & tintin solutions and none is showing last date. Here the test for tinti script:

#!/bin/ksh
TodaysDate=`date +%Y%m%d`
Yesterdaydate=`TZ=$TZ+24 date +%Y%m%d`

echo "Today: $TodaysDate"
echo "Yesterday: $Yesterdaydate"

Today: 20071004
Yesterday: 20071004

and here is my TZ

$ echo $TZ
GMT+4

and here is my Solaris version:

$ uname -a
SunOS unknown 5.8 Generic_117350-49 sun4u sparc SUNW,Sun-Blade-100
0
 
Brian UtterbackPrinciple Software EngineerCommented:
The problem is that the timezone processing code is not really designed to do arbitrary expressions involving
the timezone.  It does allow certain offset processing however. So GMT+4 is legal because it is the GMT timezone plus 4 hours. However, in the script you are using, you end up calling the date program with TZ set to the
value 'GMT+4+24'. See? This violates the formating rules and it falls back to using GMT.
0
 
ahoffmannCommented:
> ... TZ=$TZ+24
Tintin, yuzh, setting TZ this (lazy) way may result in a unknown TZ definition (somehow as blu explained)
sumanth_ora, please use either a GMT timezone for TZ or you need to find the (named) TZ which corresponds to your current setting of TZ and allows +24
Please post following:

  echo $TZ
  date
  env TZ=GMT date
  env TZ=GMT+11 date
  env TZ=GMT+23 date
  env TZ=GMT+24 date
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.