Best of CUSM: yesterdays date


What is this stuff?

If this isn't exactly what you wanted, please try our Search (there's a LOT of techy and non-techy stuff here about Linux, Unix, Mac OS X and just computers in general!):

References: <d1c185ef.0201230019.4a838487@posting.google.com> <941yfzy2hy.fsf@foo-bar-baz.cc.vt.edu>


Hate these ads?

Finding yesterday's date is easy in languages like Perl:



# this example is programmed for clarity, not efficiency
$rightnow=time();
$rightnow -= (24 * 60 * 60);
print scalar localtime($rightnow);


But in the shell it can be harder. You'll see people recommend something like:



#!/bin/ksh
yesterday=$(TZ=EST26EDT date +%mm-%d-%yy)  


where the timezone is selected for the proper effect, but that isn't actually going to work always.

Tapani Tarvainen posted a better solution:








#! /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


LOD Communications, Inc.



print $year-$month-$day
print $month/$day/${year#??}


You may also want to look at http://aplawrence.com/SCOFAQ/FAQ_scotec6datemath.html and http://aplawrence.com/Unix/yesterday.html



Comments /Bofcusm/1455.html
BofcusmNum1455 :
---July 13, 2004

The following command will do the job without any mess.

$ date --date='1 day ago'


- amber

---July 13, 2004


Thanks Amber!

It's only true on Linux, but that does point out the importance of reading "info date" instead of just "man date" - you won't find that option spelled out fully in the man page, but the info doc does explain it.

--
TonyLawrence




---August 4, 2004

The script doesn't work.
When day becomes smaller then -9, then, by the typesetting of -Z2, day == 10. While 10 !<=0, you'll get a date you've never expected.

Norbert Groen

---August 25, 2004

need any formated string? and like perl. so take:

$|=1; # flush output

$rightnow=time() - (24 * 60 * 60);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime
($rightnow);
$mon +=1; # always that

$mon = '0' . $mon if ( length $mon < 2 );

$mday = '0' . $mday if ( length $mday < 2 );

$yesterday = ( $year + 1900 ) . '.' . $mon . '.' . $mday;

print $yesterday . "\n";

exit 0;




---August 25, 2004



---November 9, 2004



---November 9, 2004



---December 8, 2004

echo $(date --date='1 day ago' +%Y%m%d)

worked fine on linux, thankx "Amber"

-merouane


---December 13, 2004

here the script of tapani in pure bash

#!/bin/bash

OFFSET=1;

eval `date "+day=%d; month=%m; year=%Y"`

# Subtract offset from day, if it goes below one use 'cal'

# to determine the number of days in the previous month.

day=`expr $day - $OFFSET`

if [ $day -le 0 ] ;then

month=`expr $month - 1`

if [ $month -eq 0 ] ;then

year=`expr $year - 1`

month=12

fi

set `cal $month $year`

xday=${$#}

day=`expr $xday + $day`

fi

echo $year-$month-$day


P.s.= i'm sorry but this editing loss indentation

--
stetor





---December 13, 2004



---December 28, 2004

Hi,
My problem is that i want to compute a date (before or after) based on a fixed date.
For example my shell parameter is : D='12/25/2004'
and i want to obtain D-5, D+2, D+4, D+5
Before starting to write a complex shell script, i want to be sure that i cannot use the date command. Any ideas ?

Thanks
Chris

Wed Mar 16 20:44:49 2005: Subject:   TonyLawrence
Robert Wolf sent email as follows:



Your script on your web page, has a bug in it, below is the corrected version. For example when you set N to a large value like 25 or 26 or 27 or 28 then depending on the current day of the month, the variable 'day' will be more than 2 digits, i.e. -10 say. Now the value '-10' will not fit in a 'typeset -Z2' variable, it needs to be 'typeset -Z3'.

#!/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 -Z3 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

typeset -Z2 day month
print $year-$month-$day
print $month/$day/${year#??}


Tue Jun 7 15:43:38 2005: Subject:   anonymous
"date --date=yesterday" or "date -d yesterday" works with 'gnu date', not only on linux.


"date --version" will tell you, if your system uses 'gnu date':

save-lin:/ # date --version
date (coreutils) 5.2.1
Written by David MacKenzie.

Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

-thomas

Mon Aug 14 14:43:00 2006: Subject:   anonymous
If you don't have gnu date, the tip at:



http://aplawrence.com/SCOFAQ/FAQ_scotec6datemath.html

is more elegant....

bil


Thu Feb 22 20:22:36 2007: Subject:   anonymous
Screw all that calculation crap. Here is a one-liner to provide yesterday's date in a shell variable.



YEST=`TZ="GMT+24" date +'%Y%m%d'`

that will provide yesterday in CCYYMMDD format. Don't like that format?
Change it by consulting strftime on your system.

Fri Feb 23 15:13:54 2007: Subject:   BigDumbDinosaur
Screw all that calculation crap. Here is a one-liner to provide yesterday's date in a shell variable.

YEST=`TZ="GMT+24" date +'%Y%m%d'`

that will provide yesterday in CCYYMMDD format. Don't like that format? Change it by consulting strftime on your system.

You can rearrange the date parameters to produce a different format. For example:

YEST=`TZ="GMT+24" date +'%m/%d/%Y'`
will produce a MM/DD/YYYY format.

If you need to go back more than a day just increment the GMT parameter by 24 per day. For example:

YEST=`TZ="GMT+72" date +'%m/%d/%Y'`
gives you the date of three days ago. To go forward, make the GMT parameter negative, e.g.:
YEST=`TZ="GMT-24" date +'%m/%d/%Y'`
gives you the date of the next day. You could also write that as:
YEST=`TZ="GMT-$((24*ND))" date +'%m/%d/%Y'`
with KSH or BASH, where ND is the number of days. The maximum range for ND is +/- 365 days.



Fri Feb 23 17:32:33 2007: Subject:   TonyLawrence
Sigh..



The TZ method was mentioned in the article body and as pointed out there and in the News articles referenced, IT WON'T ALWAYS WORK.



Fri Feb 23 18:43:19 2007: Subject:   BigDumbDinosaur
The TZ method was mentioned in the article body and as pointed out there and in the News articles referenced, IT WON'T ALWAYS WORK.

I wasn't reinventiing the wheel and I didn't say that it was 100 percent reliable. <Smile> I was merely pointing out how that method could be extended with a little twiddling.

For my applications, I use a small C program that reads STDIN for a date in YYYYMMDD format, converts it to a SQL date number, adds X number of days (which if negative, goes back in time), converts the result back to YYYYMMDD format and writes that on STDOUT. It's trustworthy from October 1752 (the first full month after the British empire adopted the Gregorian calendar) to December 31, 9999.



ad



Tue May 1 12:24:04 2007: Subject: www.iphone2die4.com   anonymous
errr.... how about:



yday=$(date --date "1 day ago")

Tue May 1 12:26:04 2007: Subject:   TonyLawrence
How about you actually READ everything written?



Add your comments

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner





Views for this page
Today This Week This Month This Year  Overall
473021,28211,346 51,036

/Bofcusm/1455.html copyright 1997-2004 (various authors) All Rights Reserved

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

More:
       - Newsgroup




Unix/Linux Consultants

Your ad here - $24.00 yearly!

http://thatitguy.com Business networking servers, Linux and Unix experts. In business since 1997! Windows and Exchange to Samba and Scalix migration experts.


SCO, OpenServer, UnixWare, software, servers, security, networks, installation, administration, troubleshooting, maintenance, Watchguard, firewalls, VPNs, e-mail. Visit us at http://opensystemscomputing.com and www.go2unix.com.


http://www.breakthru.com.au SCO (Openserver and Unixware), Unix, Solaris and Linux Consulting services including: Secure Networking Solutions; Linux based Firewalls; Backup Solutions; Secure Home to Office Network Setup; Phone, Remote and On-Site Support available - Satisfaction Guaranteed!




card_image








Change Congress


Related Posts