Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2004
    Posts
    4

    Unanswered: New Dates in Unix

    How do I derive a new date from a given date plus or minus a number of days in unix. For example:

    25-dec-2003 + 4days to give me 29-dec-2003

    Thank you very for your assistance

  2. #2
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35

    Re: New Dates in Unix

    Unix machines keep time by storing the number of seconds from the epoch as a 32-bit integer. I would determine the current time (it's now 1073657881 o'clock), add the required number of seconds and convert back. Whilst it's not strictly a shell script [1], this should do it:

    #!/path/to/perl -w
    # Takes one argument, the number of days' offset
    defined $ARGV[0] || exit 1;
    $ARGV[0] =~ /^-?\d+$/ || exit 2;
    $DBFORUMS=time;
    $DBFORUMS+=($ARGV[0]*60*60*24);
    print localtime($DBFORUMS) . "\n";
    __END__

    There's also a GNU extension to 'date' which returns the value of the number of seconds past the epoch. Depending on your system, you may have this utility and you can write a similar script in the shell.


    [1] I have considered running the perl debugger as my login shell

  3. #3
    Join Date
    Aug 2003
    Location
    Argentina
    Posts
    780

    Re: New Dates in Unix

    Originally posted by adokli
    How do I derive a new date from a given date plus or minus a number of days in unix. For example:

    25-dec-2003 + 4days to give me 29-dec-2003

    Thank you very for your assistance
    Hi,

    Please test this form

    a=4 ; expr `date +%d` + $a

    Depending on as you need use.

    Gustavo.

  4. #4
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi,
    I have a little programm that do what you ask for. (infdate.x)
    If you are intrested post a privat message. I can send it to you by Mail

    DAYS_TO_BE_ADD=4 # or back then use -4

    AKT_DATE=`date '+%d.%m.%Y'`
    INFORMIX_DATE=`infdate.x -s $AKT_DATE`
    INFORMIX_DATE=`expr $INFORMIX_DATE + $DAYS_TO_BE_ADD`
    NEW_DATE=`infdate.x -s $INFORMIX_DATE`
    echo "today ==> "$AKT_DATE
    echo "new day ==> "$NEW_DATE

    And this is the output
    --------------------------------
    today ==> 09.01.2004
    new day ==> 13.01.2004
    Greetings from germany
    Peter F.

  5. #5
    Join Date
    Mar 2004
    Posts
    1

    Lightbulb Please mail me infdate.x program

    Hi,

    I would like to use yopur small utility for date manipulation in Unix shell scripts. Please send me your program, if you dont mind.
    My e-mail address: abhandari@deloitte.com

    Thank you so much,

    Arun Bhandari.


    Originally posted by fla5do
    Hi,
    I have a little programm that do what you ask for. (infdate.x)
    If you are intrested post a privat message. I can send it to you by Mail

    DAYS_TO_BE_ADD=4 # or back then use -4

    AKT_DATE=`date '+%d.%m.%Y'`
    INFORMIX_DATE=`infdate.x -s $AKT_DATE`
    INFORMIX_DATE=`expr $INFORMIX_DATE + $DAYS_TO_BE_ADD`
    NEW_DATE=`infdate.x -s $INFORMIX_DATE`
    echo "today ==> "$AKT_DATE
    echo "new day ==> "$NEW_DATE

    And this is the output
    --------------------------------
    today ==> 09.01.2004
    new day ==> 13.01.2004

Posting Permissions

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