Yesterday date script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Yesterday date script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-05-2008
wakhan wakhan is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 18 January 2011, 11:34 PM EST
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Yesterday date script

Hello All,

I am using the below script to get yesterday date, but it is giving date of day before yesterday. Right now its 080906 but this code is giving 080904. And my requirement is 080905.


Code:
#!/bin/sh
CurrentDate=`TZ="GMT+24" date +'%y%m%d'`
echo $CurrentDate;

WHY?

Please help..

Regasrds,
Waqas Ahmed
Sponsored Links
    #2  
Old 09-05-2008
dennis.jacob's Avatar
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 30 March 2014, 9:33 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
try :


Code:
date +%Y%m%d -d "yesterday"

Sponsored Links
    #3  
Old 09-05-2008
wakhan wakhan is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 18 January 2011, 11:34 PM EST
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Yesterday date script


Code:
date +%Y%m%d -d "yesterday"

NOPE!! this is giving today's date.

Any idea about below script. sometime this script gives correct required date too.


Code:
#!/bin/sh
CurrentDate=`TZ="GMT+24" date +'%y%m%d'`
echo $CurrentDate;

    #4  
Old 09-05-2008
awk awk is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 29 May 2012, 11:56 AM EDT
Posts: 135
Thanks: 0
Thanked 0 Times in 0 Posts
Today's the 5th
Yesterday was the 4th.

I am in CST

What timezone are you in???
Sponsored Links
    #5  
Old 09-05-2008
timj123 timj123 is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 26 May 2014, 11:13 PM EDT
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
you need to add your offset as well as the time you want to go back.

Example, I am currently on CDT, my offset is +5, if you want date to display 24 hours ago, you would do the following:


Code:
$ date
Fri Sep  5 18:53:34 CDT 2008

$ y=`TZ=$TZ+29 date`
$ echo $y
Thu Sep 4 18:54:02 US/Central 2008

What timezone are you in?
Sponsored Links
    #6  
Old 09-05-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by wakhan View Post
Hello All,

Code:
#!/bin/sh
CurrentDate=`TZ="GMT+24" date +'%y%m%d'`
echo $CurrentDate;


Using the TZ variable to manipulate the date is unreliable.

If possible, use one of the versions of date that can use a different date (GNU and *BSD), or try the date shell function from Chapter 8 of my book: 8: The Dating Game
Sponsored Links
    #7  
Old 09-05-2008
RahulJoshi's Avatar
RahulJoshi RahulJoshi is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 13 September 2013, 10:47 AM EDT
Location: PUNE
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
For set to privious date:-
tz=ist+24
then for reset to origional:-
tz=tz-5:30
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
Getting yesterday's date in shell script prasperl Shell Programming and Scripting 2 02-28-2011 08:57 PM
Problem to get yesterday's date flatfile with shell script lifeseries Shell Programming and Scripting 19 10-08-2009 02:41 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 07:45 PM.