Difference between integers (Dates)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ApeX

    Difference between integers (Dates)

    hi guys!
    So here i have two arraylists with the following structure (year,
    month, day)...
    What's the best way to calculate difference between the two?

    Example

    arraylist1 - 12y, 1m, 10d
    arraylist2 - 11y, 1m, 29d

    ....i need to get a new arraylist with values (1y, 1m, 12d)

    Thnx!
  • Peter Morris

    #2
    Re: Difference between integers (Dates)

    DateTime a = new DateTime(2008, 10, 21);
    DateTime b = new DateTime(2007, 1, 7);
    TimeSpan elapsed = a - b;

    TimeSpan has various properties for extracting the number of days etc.




    --
    Pete
    ====


    Comment

    • Stefan Hoffmann

      #3
      Re: Difference between integers (Dates)

      hi,

      ApeX wrote:
      Example
      Using a fiscal year:
      arraylist1 - 12y, 1m, 10d
      = 12 * 360 + 1 * 30 + 10
      arraylist2 - 11y, 1m, 29d
      = 11 * 360 + 1 * 30 + 29
      ...i need to get a new arraylist with values (1y, 1m, 12d)
      (12 * 360 + 1 * 30 + 10) - (11 * 360 + 1 * 30 + 29) = 4360 - 4019 = 341
      =0y + 11m + 11d


      mfG
      --stefan <--

      Comment

      • Rick Lones

        #4
        Re: Difference between integers (Dates)

        ApeX wrote:
        hi guys!
        So here i have two arraylists with the following structure (year,
        month, day)...
        What's the best way to calculate difference between the two?
        >
        Example
        >
        arraylist1 - 12y, 1m, 10d
        arraylist2 - 11y, 1m, 29d
        >
        ...i need to get a new arraylist with values (1y, 1m, 12d)
        You can't, unless you have some way of knowing how many days are in the
        respective months and also which of the years are leap years. (I.e., how many
        total days are represented by each arraylist.) You really should start with
        DateTime structs and use DateTime.Subtra ct() to generate a TimeSpan difference.

        HTH,
        -rick-

        Comment

        Working...