Getting the yesterday date number

Getting the yesterday date number

Post by LLOY » Fri, 12 Apr 2002 19:20:23



Hi all,

Does anyone now how to get the yesterday's date on a ksh ?
If I do this example it works but when the date is the first of the
month I get 00 instead of 31 or 30
Yes I now, it's quite simple but work's fine the rest of the month
:-)

Ex:

D=`date +"%Y%m%d"`
echo $D
20020411

YD=`expr ${D} - 1`
echo $YD
20020410

Thanks

LMC

 
 
 

Getting the yesterday date number

Post by David Near » Fri, 12 Apr 2002 19:31:28


On 11 Apr 2002 03:20:23 -0700, LLOYS said:

Quote:> Hi all,

> Does anyone now how to get the yesterday's date on a ksh ?
> If I do this example it works but when the date is the first of the
> month I get 00 instead of 31 or 30 …
> Yes I now, it's quite simple but work's fine the rest of the month
> … :-)

> Ex:

> D=`date +"%Y%m%d"`
> echo $D
> 20020411

> YD=`expr ${D} - 1`
> echo $YD
> 20020410

Does
D=`date +"%Y%m%d" -d "yesterday"`
work?

Dave.

--
          David Neary,
  E-Mail: bolsh at gimp dot org

 
 
 

Getting the yesterday date number

Post by Andrey Koubyche » Fri, 12 Apr 2002 20:37:01


Hi,
Answer from someone in comp.unix.shell :

x=`date`
sleep 85400
echo $x

:-D

--
Best regards,
Andrey Koubychev

 
 
 

Getting the yesterday date number

Post by LMC » Fri, 12 Apr 2002 20:36:55


 What do you mean by "yesterday" ?
 Do you have an example ?

Thanks



Quote:> On 11 Apr 2002 03:20:23 -0700, LLOYS said:
> > Hi all,

> > Does anyone now how to get the yesterday's date on a ksh ?
> > If I do this example it works but when the date is the first of the
> > month I get 00 instead of 31 or 30 …
> > Yes I now, it's quite simple but work's fine the rest of the month
> > … :-)

> > Ex:

> > D=`date +"%Y%m%d"`
> > echo $D
> > 20020411

> > YD=`expr ${D} - 1`
> > echo $YD
> > 20020410

> Does
> D=`date +"%Y%m%d" -d "yesterday"`
> work?

> Dave.

> --
>           David Neary,
>   E-Mail: bolsh at gimp dot org

 
 
 

Getting the yesterday date number

Post by LMC » Fri, 12 Apr 2002 21:01:03


Many thanks for your great help you saved my day ... {?}



Quote:> Hi,
> Answer from someone in comp.unix.shell :

> x=`date`
> sleep 85400
> echo $x

> :-D

> --
> Best regards,
> Andrey Koubychev

 
 
 

Getting the yesterday date number

Post by ?yvind R?tvol » Fri, 12 Apr 2002 21:08:24



> Hi all,

> Does anyone now how to get the yesterday's date on a ksh ?

 $ date
Thursday April 11 14:05:36 MET DST 2002
 $ export TZ='MET+24'
 $ date
Wednesday April 10 12:05:43 MET 2002

Close but not quite, the rest is left as an excercise for the reader,
(or closer to the truth: I'm too lazy).

[snip]

--
?yvind R?tvold, URL: http://palpatine.darkside.no/olr/index.html

 
 
 

Getting the yesterday date number

Post by LMC » Fri, 12 Apr 2002 21:20:10


Thanks ... ?yvind R?tvold

Simple and efficient .

LMC




> > Hi all,

> > Does anyone now how to get the yesterday's date on a ksh ?

>  $ date
> Thursday April 11 14:05:36 MET DST 2002
>  $ export TZ='MET+24'
>  $ date
> Wednesday April 10 12:05:43 MET 2002

> Close but not quite, the rest is left as an excercise for the reader,
> (or closer to the truth: I'm too lazy).

> [snip]

> --
> ?yvind R?tvold, URL: http://palpatine.darkside.no/olr/index.html

 
 
 

Getting the yesterday date number

Post by Ralf Fasse » Fri, 12 Apr 2002 21:40:32



| Does anyone now how to get the yesterday's date on a ksh ?

ksh alone?  No.  Other than that, I'd use some utility which
understands printing the date for a given time, eg gawk or perl.

gawk 'BEGIN {print strftime("%Y%m%d", systime()-86400); exit}'
perl -e 'print localtime(time()-86400) . "\n"'
perl -e 'use POSIX; print strftime("%Y%m%d\n",localtime(time()-86400))'

R'

 
 
 

Getting the yesterday date number

Post by David Near » Fri, 12 Apr 2002 21:51:54


On Thu, 11 Apr 2002 13:36:55 +0200, LMC said:

Quote:>  What do you mean by "yesterday" ?
>  Do you have an example ?

I mean the string "yesterday" passed to the -d option of date.

I have gnu date (which is different to yours, certainly), and it
handles relative dates. It parses some very unusual ones too -
last friday, for example, works.

A few examples are below. I thought that maybe your "date"
supported relative times for -d

Cheers,
Dave.


20020411

"yesterday" +"%Y%m%d"
20020410

20020405

20020410

--
          David Neary,
  E-Mail: bolsh at gimp dot org

 
 
 

Getting the yesterday date number

Post by LMC » Fri, 12 Apr 2002 21:53:16


 Works fine Ralf, thanks.
The gawk is very interesting.

I now a bit of awk but not gawk .

 Thanks anyway for your great help.

 LMC




> | Does anyone now how to get the yesterday's date on a ksh ?

> ksh alone?  No.  Other than that, I'd use some utility which
> understands printing the date for a given time, eg gawk or perl.

> gawk 'BEGIN {print strftime("%Y%m%d", systime()-86400); exit}'
> perl -e 'print localtime(time()-86400) . "\n"'
> perl -e 'use POSIX; print strftime("%Y%m%d\n",localtime(time()-86400))'

> R'

 
 
 

Getting the yesterday date number

Post by Andrew Gabri » Fri, 12 Apr 2002 22:36:10




Quote:> Hi,
> Answer from someone in comp.unix.shell :

> x=`date`
> sleep 85400
> echo $x

There's a bug in this - it won't work during the
first 16 minutes and 40 seconds of each day - you
will get the same date returned.

Try 86400 instead of 85400. That probably reduces
the window of error down to 1 second rounding error.

;-)

--
Andrew Gabriel

 
 
 

Getting the yesterday date number

Post by LMC » Fri, 12 Apr 2002 22:49:16


Thanks Dave for spending time by giving this examples.
Unfortunately I have a "Compaq Tru64 UNIX V5.1A" and these examples do not
work for me, I've found already a few examples that work fine.

Thanks again for your help.

LMC



> On Thu, 11 Apr 2002 13:36:55 +0200, LMC said:
> >  What do you mean by "yesterday" ?
> >  Do you have an example ?

> I mean the string "yesterday" passed to the -d option of date.

> I have gnu date (which is different to yours, certainly), and it
> handles relative dates. It parses some very unusual ones too -
> last friday, for example, works.

> A few examples are below. I thought that maybe your "date"
> supported relative times for -d

> Cheers,
> Dave.


> 20020411

> "yesterday" +"%Y%m%d"
> 20020410

> 20020405

> 20020410

> --
>           David Neary,
>   E-Mail: bolsh at gimp dot org

 
 
 

Getting the yesterday date number

Post by Ralf Fasse » Sat, 13 Apr 2002 04:29:36



| I now a bit of awk but not gawk .

gawk is GNU awk, awk extended by some useful functions (like
strftime).   http://www.gnu.org/software/gawk/gawk.html

R'

 
 
 

Getting the yesterday date number

Post by Andrew Giert » Sun, 14 Apr 2002 01:44:19


 LLOYS> Hi all,
 LLOYS> Does anyone now how to get the yesterday's date on a ksh ?

A POSIX shell brute-force solution:

# yesterday %d %m %Y
# note: expects two-digit day and month fields

yesterday() {
    case "$1$2" in
    0101*)  set -- 31 12 $(($3-1));;
    01*)    set -- 00 $(($2-1)) $3
            case "$2" in
            2)       set -- $((28+($3%4==0))) 02 $3;;
            [13578]) set -- 31 0$2 $3;;
            [469])   set -- 30 0$2 $3;;
            10)      set -- 31 10 $3;;
            11)      set -- 30 11 $3;;
            esac;;
    0*|10*) set -- 0$(($1-1)) $2 $3;;
    *)      set -- $(($1-1)) $2 $3;;
    esac
    echo "$1 $2 $3"

Quote:}

e.g.
set -- $(yesterday $(date +'%d %m %Y'))

Two incorrect solutions are commonly given every time this question is
asked here, one of them being specific to GNU date (passing
nonstandard options to the date command) and the other being
nonportable (using illegal timezone offsets, which works on some
systems but not others).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Getting the yesterday date number

Post by Kenny McCorma » Tue, 16 Apr 2002 11:31:53




...

Quote:>Two incorrect solutions are commonly given every time this question is
>asked here, one of them being specific to GNU date (passing
>nonstandard options to the date command) and the other being
>nonportable (using illegal timezone offsets, which works on some
>systems but not others).

I find it curious that you call using GNU date an incorrect solution.
In my opinion, it is the only correct solution.  If you are going to do any
kind of serious shell scripting, you need GNU date (*) in order to handle
the date manipulation, so you might as well bite the bullet, get it, and
get it over with.  You'll be glad you did.

(*) Or some other compiled utility, such as ADU.  But, when all is said and
done, GNU date is the most straightforward solution.

Oh, and, by the way, here is a shell script to display yesterday's date,
that should work, more or less, on any system.  Note: This is largely a
joke and is psuedo-code, so don't both writing back and telling me that you
couldn't get it to work...

#!/bin/sh
set -e
cd /tmp
wget ftp://ftp.gnu.org/sh-utils/sh-utils.tgz
tar xzvf sh-utils.tgz
cd sh-utils
./configure;make;./date -d yesterday

 
 
 

1. Getting the yesterday date number

Hi all,

Does anyone now how to get the yesterday's date on a ksh ?
If I do this example it works but when the date is the first of the
month I get 00 instead of 31 or 30
Yes I now, it's quite simple but work's fine the rest of the month
:-)

Ex:

D=`date +"%Y%m%d"`
echo $D
20020411

YD=`expr ${D} - 1`
echo $YD
20020410

Thanks

2. purify on Solaris Linker error

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

4. panic : NMI on SPARCstation 4

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

6. USB hotplug cannot detect device removal

7. Getting Yesterday's Date

8. Looking for FMLI replacement

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

10. Get "date '+%y%m%d'" for yesterday?

11. How do I get yesterdays date?

12. How can I get yesterdays Date