Professional Web Applications Themes

Question about the date calculation - Linux / Unix Administration

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. #1

    Default 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?

    Many thanks in advance

    Herbert Guest

  2. #2

    Default 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. #3

    Default Re: Question about the date calculation

    Herbert
    Dec 22, 4:44 pm show options

    Newsgroups: comp.unix.admin
    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?


    Many thanks in advance


    Reply





    Bit Twister
    Dec 22, 5:12 pm show options

    Newsgroups: comp.unix.admin
    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. #4

    Default 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
    some helpful FAQS.

    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. #5

    Default 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
    instead of using google.

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

    Depends on your shell.

     

    How about

    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. #6

    Default Re: Question about the date calculation

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

    This might help you with scripts.

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

  7. #7

    Default 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. #8

    Default 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

Similar Threads

  1. Date calculation in ActionScript
    By gregd in forum Coldfusion Flash Integration
    Replies: 2
    Last Post: February 25th, 01:25 AM
  2. Date calculation
    By pugs in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 1st, 12:36 PM
  3. Date and time calculation
    By Werner in forum PERL Beginners
    Replies: 5
    Last Post: June 17th, 06:57 PM
  4. Date calculation
    By Jan in forum PERL Beginners
    Replies: 9
    Last Post: April 22nd, 03:42 PM
  5. Find Date in Calculation field
    By Bigfoot1956 in forum FileMaker
    Replies: 3
    Last Post: July 21st, 09:40 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139