Hello, I am trying to calculate date like below: date +%Y%m%d -4 if the date 20051201, the result should be 20051127. Could you anyone help me with this issue please? How can I get the right result? Many thanks in advance...

1. ## Question about the date calculation

Hello,

I am trying to calculate date like below:

date +%Y%m%d -4

if the date 20051201, the result should be 20051127.

Could you anyone help me with this issue please? How can I get the
right result?

Herbert Guest

2. ## Re: Question about the date calculation

On 22 Dec 2005 12:44:30 -0800, Herbert wrote:

would something like this work on your system? This is what happens on mine

\$ date +%Y%m%d
20051222

\$ date --date="now + -23 days" +%Y%m%d
20051129

Bit Guest

3. ## Re: Question about the date calculation

Herbert
Dec 22, 4:44 pm show options

From: "Herbert" <com> - Find messages by this author
Date: 22 Dec 2005 12:44:30 -0800
Local: Thurs, Dec 22 2005 4:44 pm
Subject: Question about the date calculation
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

Hello,

I am trying to calculate date like below:

date +%Y%m%d -4

if the date 20051201, the result should be 20051127.

Could you anyone help me with this issue please? How can I get the
right result?

Bit Twister
Dec 22, 5:12 pm show options

From: Bit Twister <com> - Find messages by this
author
Date: Thu, 22 Dec 2005 15:12:07 -0600
Local: Thurs, Dec 22 2005 5:12 pm
Subject: Re: Question about the date calculation
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

On 22 Dec 2005 12:44:30 -0800, Herbert wrote:

would something like this work on your system? This is what happens on
mine

\$ date +%Y%m%d
20051222

\$ date --date="now + -23 days" +%Y%m%d
20051129

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

It worked fine on our system too. One more Q. How do I make it like
below;

NEW_DATE=date --date="now + -23 days" +%Y%m%d

echo \$NEW_DATE

and can you also explain how it(--date) works please?

Thanks

Herbert Guest

4. ## Re: Question about the date calculation

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In comp.unix.admin, Herbert dared to utter,

date arithmetic is one of those black arts that few people fully
understand. From the information you've given, it's pretty difficult
to tell you what will work.

Thankfully, better people than you or I have done this before and wrote

http://home.comcast.net/~j.p.h/cus-faq.html#6

- --
It is better to hear the rebuke of the wise,
Than for a man to hear the song of fools.
Ecclesiastes 7:5
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDqxujz8zcalmVmBkRAvQMAJ9cFmYeXaJ3Y92Dfc8qYP VydVRIVQCgmNhj
QNsH5dKGvuciIRZHNagk8pg=
=EVfQ
-----END PGP SIGNATURE-----
+Alan Guest

5. ## Re: Question about the date calculation

On 22 Dec 2005 13:32:32 -0800, Herbert wrote:

Sure wish you would get a good news reader and use a news server

If you are going to continue to use google to post, TRIM YOUR POSTS.

NEW_DATE=\$(date --date="now + -23 days" +%Y%m%d)

or use backtics (Acent grave, not apostrophes)
NEW_DATE=`date --date="now + -23 days" +%Y%m%d`

It works pretty good.

Have you tried man date
and if you have it info coreutils date

if not, here

27.6 Relative items in date strings
===================================

"Relative items" adjust a date (or the current date if none) forward or
backward. The effects of relative items aculate. Here are some
examples:

1 year
1 year ago
3 years
2 days

The unit of time displacement may be selected by the string `year'
or `month' for moving by whole years or months. These are fuzzy units,
as years and months are not all of equal duration. More precise units
are `fortnight' which is worth 14 days, `week' worth 7 days, `day'
worth 24 hours, `hour' worth 60 minutes, `minute' or `min' worth 60
seconds, and `second' or `sec' worth one second. An `s' suffix on
these units is accepted and ignored.

The unit of time may be preceded by a multiplier, given as an
optionally signed number. Unsigned numbers are taken as positively
signed. No number at all implies 1 for a multiplier. Following a
relative item by the string `ago' is equivalent to preceding the unit
by a multiplier with value -1.

The string `tomorrow' is worth one day in the future (equivalent to
`day'), the string `yesterday' is worth one day in the past (equivalent
to `day ago').

The strings `now' or `today' are relative items corresponding to
zero-valued time displacement, these strings come from the fact a
zero-valued time displacement represents the current time when not
otherwise changed by previous items. They may be used to stress other
items, like in `12:00 today'. The string `this' also has the meaning
of a zero-valued time displacement, but is preferred in date strings
like `this thursday'.

When a relative item causes the resulting date to cross a boundary
where the clocks were adjusted, typically for daylight-saving time, the
resulting date and time are adjusted accordingly.

The fuzz in units can cause problems with relative items. For
example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:

\$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
\$ date --date="-1 month" +'Last month was %B?'
Last month was July?
\$ date --date="\$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!

Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting the `TZ' environment variable to `UTC0'
before embarking on calendrical calculations.
Bit Guest

6. ## Re: Question about the date calculation

On 22 Dec 2005 13:32:32 -0800, Herbert wrote:

! bash script introduction doentation
http://tldp.org/LDP/intro-linux/html/index.html
http://tldp.org/LDP/abs/html/index.html
Bit Guest

7. ## Re: Question about the date calculation

On 2005-12-22, Herbert <com> wrote:

#!/bin/sh

days=4
tz=`expr 24 * \$days`
date=`(TZ=\$tz date '+%Y%m%d')`
Mike Guest

8. ## Re: Question about the date calculation

On Thu, 22 Dec 2005 15:32:41 -0600 +Alan Hicks+ <network> wrote:

| date arithmetic is one of those black arts that few people fully
| understand. From the information you've given, it's pretty difficult
| to tell you what will work.

One thing that can readily trip up some people trying to do their own
arithmetic implementations is the fact that the C language, and probably
many others, assume or standardize on "truncation towards zero" as the
type of division being done. But date/time arithmetic needs "truncation
towards negative infinity" type of division when negative values may be
involved (e.g. before the chosen epoch).

Consider the following range of values being each divided by 4:
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9

The following is the result of division towards zero:
quotient -2 -2 -1 -1 -1 -1 0 0 0 0 0 0 0 1 1 1 1 2 2
remainder -1 0 -3 -2 -1 0 -3 -2 -1 0 1 2 3 0 1 2 3 0 1

The following is the result of division towards negative infinity:
quotient -3 -2 -2 -2 -2 -1 -1 -1 -1 0 0 0 0 1 1 1 1 2 2
modulus 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1

Time calculations involving periods should work the same when shifted
an exact number of periods. That doesn't happen when the shift moves
to the "before epoch" range and involves the negtive value when division
towards zero is used. But division towards negative infinity makes it
work out transparently.

I have the following C macro to deal with this:

http://libh.slashusr.org/source/div/src/lib/h/mod_ni.h

--
-----------------------------------------------------------------------------
| Phil Howard KA9WGN | http://linuxhomepage.com/ http://ham.org/ |
| (first name) at ipal.net | http://phil.ipal.org/ http://ka9wgn.ham.org/ |
-----------------------------------------------------------------------------
phil-news-nospam@ipal.net Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•