HELP: shell script to get yesterdays date

HELP: shell script to get yesterdays date

Post by John Wrig » Wed, 28 Aug 1996 04:00:00



I've been using
DATE = `date +%y%m%d`
to print the date in YYMMDD format, but what I'd like to do is
use this (or another simple way) to get yesterdays date in the
same format.

If there is a way to do it in perl, that is acceptable too.
I could write a routine to figure it out, but if there is a short hand
way of determining it, that would be best.

Thanks,

Jay

 
 
 

HELP: shell script to get yesterdays date

Post by Joe Sei » Wed, 28 Aug 1996 04:00:00


|> I've been using
|> DATE = `date +%y%m%d`
|> to print the date in YYMMDD format, but what I'd like to do is
|> use this (or another simple way) to get yesterdays date in the
|> same format.
|>
|> If there is a way to do it in perl, that is acceptable too.
|> I could write a routine to figure it out, but if there is a short hand
|> way of determining it, that would be best.
|>
|> Thanks,
|>
|> Jay
|>

Add 24 to the offset in your TZ enviroment variable, e.g.

TZ=EST29EDT date +%y%m%d



 
 
 

HELP: shell script to get yesterdays date

Post by Randal L. Schwar » Wed, 28 Aug 1996 04:00:00


John> I've been using
John> DATE = `date +%y%m%d`
John> to print the date in YYMMDD format, but what I'd like to do is
John> use this (or another simple way) to get yesterdays date in the
John> same format.

John> If there is a way to do it in perl, that is acceptable too.
John> I could write a routine to figure it out, but if there is a short hand
John> way of determining it, that would be best.

        perl -e 'printf "%02d%02d%02d", (localtime time-86400)[5,4,3]'

Just another Perl hacker,
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

 
 
 

HELP: shell script to get yesterdays date

Post by Bruce W. Hoylm » Fri, 30 Aug 1996 04:00:00


The GNU date() utility has a '--date' option that accepts a string as a
value.  The command will subsequently return the date associated with
the string value.  Valid string values are "yesterday" and "2 days ago",
among others.

Ain't GNU wonderful!

 
 
 

HELP: shell script to get yesterdays date

Post by Doc O'Lea » Sat, 31 Aug 1996 04:00:00



>|> I've been using
>|> DATE = `date +%y%m%d`
>|> to print the date in YYMMDD format, but what I'd like to do is
>|> use this (or another simple way) to get yesterdays date in the
>|> same format.

I have yet to see the "right" answer to this.  Use:

        date -d yesterday +%y%m%d

--
Copyright 1996 by Doc O'Leary.
Author of the wildly unsuccessful "DOS and Windows for People Who
Still Have a Clue"

 
 
 

HELP: shell script to get yesterdays date

Post by Doug Prob » Sat, 31 Aug 1996 04:00:00




}

} >|> I've been using
} >|> DATE = `date +%y%m%d`
} >|> to print the date in YYMMDD format, but what I'd like to do is
} >|> use this (or another simple way) to get yesterdays date in the
} >|> same format.
}
} I have yet to see the "right" answer to this.  Use:
}
}       date -d yesterday +%y%m%d
}

That might be "right" on you OS but is dont work on SCO 3.2.2

--
--------------------------------------------------------------------------------


 
 
 

HELP: shell script to get yesterdays date

Post by Alessandro Pitti » Sat, 31 Aug 1996 04:00:00



Quote:(John Wright) writes:
> I've been using
> DATE = `date +%y%m%d`
> to print the date in YYMMDD format, but what I'd like to do is
> use this (or another simple way) to get yesterdays date in the
> same format.

Try experiment with TZ variable.
In my country TZ=GMT+22 works fine. (except maybe for DST)

Hope this help.

Ciao, Alex.

 
 
 

HELP: shell script to get yesterdays date

Post by John Hende » Sat, 31 Aug 1996 04:00:00



Quote:>}   date -d yesterday +%y%m%d
>}
>That might be "right" on you OS but is dont work on SCO 3.2.2

In fact, none of the suggestions work for SCO's date. Fiddling with the
TZ variable causes it to print the current date in Universal time,

Time to install gnu date.

--
      Artificial Intelligence stands no chance against Natural Stupidity.
                GAT d- -p+(--) c++++ l++ u++ t- m--- W--- !v
                     b+++ e* s-/+ n-(?) h++ f+g+ w+++ y*

 
 
 

HELP: shell script to get yesterdays date

Post by Lawson Hans » Mon, 02 Sep 1996 04:00:00




>(John Wright) writes:
>> I've been using
>> DATE = `date +%y%m%d`
>> to print the date in YYMMDD format, but what I'd like to do is
>> use this (or another simple way) to get yesterdays date in the
>> same format.
>Try experiment with TZ variable.
>In my country TZ=GMT+22 works fine. (except maybe for DST)
>Hope this help.
>Ciao, Alex.

If you have Perl-5 installed, then you can use:

#!/bin/sh
#
#  Program: yesterday
#   Author: Randall Schwartz
#  Purpose: Prints yesterday's date
#
perl -e 'printf "%s\n", scalar localtime (time - 86400)'

Hope that helps.

Best regards,

Lawson Hanson

 
 
 

HELP: shell script to get yesterdays date

Post by Randal L. Schwar » Tue, 03 Sep 1996 04:00:00


Lawson> If you have Perl-5 installed, then you can use:

Lawson> #!/bin/sh
Lawson> #
Lawson> #  Program: yesterday
Lawson> #   Author: Randall Schwartz
Lawson> #  Purpose: Prints yesterday's date
Lawson> #
Lawson> perl -e 'printf "%s\n", scalar localtime (time - 86400)'

Ouch.  That hurts, unless you're talking about some other Randall [sic]
Schwartz.

My version was merely:

        perl -e 'print scalar localtime (time - 86400)'

No need to make the trip into printf land.  (My friends would be so
embarassed for me if they thought I wrote code like that. :-)

Just another Perl hacker,
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

 
 
 

HELP: shell script to get yesterdays date

Post by Rachel Polansk » Wed, 04 Sep 1996 04:00:00





> Lawson> If you have Perl-5 installed, then you can use:

> Lawson> #!/bin/sh
> Lawson> #
> Lawson> #  Program: yesterday
> Lawson> #   Author: Randall Schwartz
> Lawson> #  Purpose: Prints yesterday's date
> Lawson> #
> Lawson> perl -e 'printf "%s\n", scalar localtime (time - 86400)'

> Ouch.  That hurts, unless you're talking about some other Randall [sic]
> Schwartz.

> My version was merely:

>    perl -e 'print scalar localtime (time - 86400)'

> No need to make the trip into printf land.  (My friends would be so
> embarassed for me if they thought I wrote code like that. :-)

> Just another Perl hacker,

While perl is wonderful tool,
it's a sledgehammer approach to this problem.

Use GNU date:

/opt/local/gnu/bin/date --date '2 days ago'

And it almost looks like it was written by a human being ;)
Assuming of course (like perl) you have it on your system!

In UNIX, there is always one more way to defer a feline ;)
(not a useless use of cat)

sorry ;)

rachel

--
Rachel Polanskis                 Kingswood, Greater Western Sydney, Australia


                Witty comment revoked due to funding cuts

 
 
 

HELP: shell script to get yesterdays date

Post by Dr A. N. Walke » Thu, 05 Sep 1996 04:00:00



> > Lawson> perl -e 'printf "%s\n", scalar localtime (time - 86400)'
> >       perl -e 'print scalar localtime (time - 86400)'
> /opt/local/gnu/bin/date --date '2 days ago'

TZ=GMT24 date
   ^^^^^
        or BST23 or PDP11 or GMT24BST,M3.4.0/1,M10.4.0 or whatever.
Save electrons, coloured pixels, and 30-odd characters of typing!
Only incredibly *ic applications will need to do more subtle
piggling with the TZ variable [in which case the other solutions
may be easier].

--
Andy Walker, Maths Dept., Nott'm Univ., UK.

 
 
 

HELP: shell script to get yesterdays date

Post by Geoff Kingsmil » Fri, 06 Sep 1996 04:00:00


Quote:> Use GNU date:

> /opt/local/gnu/bin/date --date '2 days ago'

How do you get the following using GNU's date:-

- last day of last month
- last day of February last year

Thanks,
Geoff..

 
 
 

HELP: shell script to get yesterdays date

Post by Gary F. Leh » Sat, 07 Sep 1996 04:00:00


Here's a relatively simple shell script to get yesterday's
date.  This solution hit me over the head about a month ago.

:
# calculate yesterdays date
echo "Now is `date`"
p=`echo $TZ | cut -c1-3`
d=`echo $p | tr S D`
o=`echo $TZ | tr -d [A-Z]`
y=`expr $o + 24`
TZ=`echo $p$y$d`
echo "Yesterday was `date`"

 
 
 

HELP: shell script to get yesterdays date

Post by Greg Wooledge x59 » Fri, 13 Sep 1996 04:00:00



Quote:># calculate yesterdays date
>echo "Now is `date`"
>p=`echo $TZ | cut -c1-3`
>d=`echo $p | tr S D`
>o=`echo $TZ | tr -d [A-Z]`
>y=`expr $o + 24`
>TZ=`echo $p$y$d`
>echo "Yesterday was `date`"

Not portable. :-(  There is one blank line before each command below:

$ uname -a; echo $TZ
SunOS techsrv1 5.5 Generic sun4c sparc SUNW,Sun_4_50
US/Eastern

$ uname -a; echo $TZ
OSF1 rtdu56.bpw.ous.bp.com V3.2 148 alpha

$ uname -a; echo $TZ
Linux techsrv10 2.0.0 #1 Tue Jun 25 12:14:29 GMT 1996 i486

$ uname -a; echo $TZ
ULTRIX www.ous.bp.com 4.4 0 RISC

and so on....  But on systems where this does work, it's rather clever.
I like it. :-)

--

UNIX Technical Support                  (216) 586-5932
British Petroleum                       FAX (216) 586-2044

"The views expressed in this note are those of the individual concerned and
should not necessarily be taken as being the views of The British Petroleum
Company p.l.c. or any of the BP Group of companies."

 
 
 

1. Korn shell date cmd - getting yesterday's date

Is there an easy way of figuring yesterday's date based on using the
$(date) function.  The problems comes into play when I am trying to
pull a file with yesterday's date and today happens to be the 1st of
the month.  How can I figure out what the last day of the previous
month was?  I know there is an easy way in perl to do this, but I was
hoping there was a trick to it in Korn shell.

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Solaris : Is there a list of known problems ?

3. Yesterdays's date from a shell script

4. XConfig specs for Logitech mouse

5. shell script to display yesterday's date and time?

6. Two NIC's

7. Yesterdays date in a shell script

8. RedHat Kernel compile not working

9. Yesterdays date in a shell script. Any ideas?

10. Calculating yesterday's date using GNU 'date' (was Re: Require a script)

11. Help:Script to show today and yesterday date.

12. Get day before yesterday's and yesterday's date.

13. Getting the yesterday date number