Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2004
    Posts
    31

    Unanswered: How to increment dates in ksh script ?

    Is there a way (in the Korn Shell) to accurately increment a date ?



    Say for example that I awk out the following "start_date" and "end_date" (in the format below) from a file:

    start_date = 10/25/2004

    end_date = 11/12/2004

    How do I increment from the start_date to the end_date ?
    (also note the change in months for the dates it is also possible that the years could be different)

    I wuz given an example like this though :


    my_date=`TZ=EST-24 date` # where date is the system's date (assume that date returned Mon Oct 25 15:51:18 EST 2004)

    echo $my_date # my_date returns Tue Oct 26 20:53:08 EST 2004


    # Each time I subtract 24 the date is incremented
    (The above seem to work but I don't really understand it)

    For my purposes I could very well work with this "solution"....except for the little problem that the dates that I would be feeding the script would look like :


    10/26/2004 NOT Tue Oct 26 20:53:08 EST 2004


    Is there a way to convert 10/26/2004 TO Tue Oct 26 20:53:08 EST 2004
    Last edited by bluwulf; 10-25-04 at 18:02.

  2. #2
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi,
    may be this can help.
    http://www.dbforums.com/t1004206.html
    Greetings from germany
    Peter F.

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Try...
    Code:
    TZ=EST-24 date +%d/%m/%Y
    ...and you aren't subtracting 24 hours from the date, you are setting theTimeZone to be "EST-24" which is interpreted as 24 hours after EST.

    btw This isn't a particularly portable, so don't be surprised if it doesn;t work in every shell you use!

  4. #4
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    ################################################## ###################
    # Example for your application
    # start_date = 10/25/2004
    # end_date = 11/12/2004
    ################################################## ###################

    ######## example to use idate.sc ####################################
    ######## Remember it is the german Format // Day / Month / Year // ##

    start_date="25.10.2004"
    end___date="12.11.2004"

    echo "Start with "$start_date" and end with "$end___date
    echo "--------------------------------------------------"

    INF_start_date=`idate.sc $start_date`
    INF_end___date=`idate.sc $end___date`
    days_between_s_e=`expr $INF_end___date - $INF_start_date`

    echo "INFORMIX Start_Date "$INF_start_date
    echo "INFORMIX End_Date "$INF_end___date
    echo "Days between Start and End "$days_between_s_e

    echo "------- and now the way back ---------------------"
    echo "--------------------------------------------------"

    x=`expr $INF_end___date - 18`
    start_date=`idate.sc $x`
    y=`expr $INF_start_date + 18`
    end_date=`idate.sc $y`

    echo "Start_Date "$start_date
    echo "End_Date "$end_date

    ##### And this will be the output ###################
    ##### for this example ###################
    #
    #Start with 25.10.2004 and end with 12.11.2004
    #--------------------------------------------------
    #INFORMIX Start_Date 38284
    #INFORMIX End_Date 38302
    #Days between Start and End 18
    #------- and now the way back -----------------
    #----------------------------------------------
    #Start_Date 25.10.2004
    #End_Date 12.11.2004
    Greetings from germany
    Peter F.

  5. #5
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138

    Smile

    More informations to idate.sc

    My english ist not so good. So I want to explain the handling again.

    idate.sc have one parameter.

    STEP1
    If you use an integer value like this "idate.sc 38284" you get back the date in this format "25.10.2004" (dd.mm.yyyy)

    If you use a date value like this "idate.sc 25.10.2004" (dd.mm.yyyy) you get back an integer value in this format "38284" . Now you can add or subtract this value and give it back to STEP1 to get the date you have manipulate.

    Bye
    Greetings from germany
    Peter F.

  6. #6
    Join Date
    Oct 2004
    Posts
    31
    I jus would like to say thanx for all the suggestions thus far.

Posting Permissions

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