[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get the yesterday's date based on sysdate

Posted on 2007-10-02
20
Medium Priority
?
1,597 Views
Last Modified: 2013-12-27
How to get the yeterday's date base on the sysdate using shell programming
0
Comment
Question by:sumanth_ora
  • 6
  • 4
  • 4
  • +4
20 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 20003948
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20004266
if you have GNU date

date --date='1 day ago'
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20005131
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 20005487
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
 
LVL 38

Expert Comment

by:yuzh
ID: 20005580
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20005722
oops, I meant yuzh's suggestion, obviously ...
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20008202
Hi,

Thank you both, but I did copy & paste and I still not able to get previous day date.
0
 

Author Comment

by:sumanth_ora
ID: 20008227
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20010399
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20011195
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 20011205
Hi,

Tintin:

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

Thanks,
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20011227
Definitely not a typo.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20011236
Hi,

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

0
 
LVL 48

Expert Comment

by:Tintin
ID: 20011261
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
 
LVL 38

Accepted Solution

by:
yuzh earned 2000 total points
ID: 20011668
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20011711
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
 
LVL 38

Expert Comment

by:yuzh
ID: 20011746
Tintin,
    Good memory! Yes, you are my next door neighbor.
    Have a nice day, cheers!
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20012751
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
 
LVL 22

Expert Comment

by:Brian Utterback
ID: 20013452
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20019946
> ... 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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question