Results 1 to 7 of 7

Thread: Converting days to yy/mm/dd (Resolved)

  1. #1

    Thread Starter
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265

    Converting days to yy/mm/dd (Resolved)

    Hi all,

    Having had great success with what I thought were going to be the hard bits, I left what I thought was going to be trivially easy till last. I should have known better

    VB Code:
    1. Dy = Math.Abs(DateDiff(DateInterval.Day, DateOne, DateTwo))

    That nicely gives the result in days. What I want is the result in Years, Months, and Days. I'm just about to give up and write a function or a class to do it, but before I do I want to make sure that I'm not missing some easy way to convert days into Y,M,D. I've been through MSDN Library looking at Date, DateTime, TimeSpan, DatePart until my eyes glazed over, but I can't figure out an easy way of doing it.

    Any ideas?
    Last edited by SuperSparks; Mar 4th, 2004 at 03:22 PM.
    Nick.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's a link to an example i made ( 2nd post down , as Csharp ) , it does the years / months , all you need to add to it is the days ( same principle as the year / month )
    [ Link to thread ]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Timespan should do it.
    Code:
            Dim ts As TimeSpan = New TimeSpan(3000, 0, 0, 0)
            MsgBox(ts.TotalDays)
            MsgBox(ts.TotalHours)
            MsgBox(ts.TotalMinutes)
            MsgBox(ts.TotalSeconds)
            MsgBox(ts.TotalMilliseconds)
    oh wait you want months..
    Last edited by Dmyze; Mar 3rd, 2004 at 09:01 PM.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  4. #4

    Thread Starter
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265
    Many thanks, guys

    That link of dynamic_sysop's looks like it will work nicely. Rather annoyingly I'd already tried something along the same lines, but couldn't get it to work, so I convinced myself that I was barking up the wrong tree. That'll teach me.

    Dynamic, I was interested that you said that DateDiff was a VB6 leftover in the other thread - I've used it elsewhere in my my code and I'm not happy with it. I'm trying hard to do things the .NET way, but MSDN Library doesn't seem to differentiate between the old and the new. Is there a list of what is legacy stuff that I can refer to anywhere?
    Nick.

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    these are the visual basic functions / classes that exist in vb.net , but are left over ( use a reference to visual basic )
    Visual Basic Run-Time Library Members
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265
    Excellent, thank you kindly. I'll send you a few hours of virtual sleep by way of thanks
    Nick.

  7. #7

    Thread Starter
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265
    After many hours of clicking the Step-into button to try and work out all the logic errors, here is the finished code block. It will take any two dates and display the difference in Years, Months and Days. Hopefully it will come in useful to someone else:

    VB Code:
    1. Private Function Y_M_D_Diff(ByVal DateOne As DateTime, ByVal DateTwo As DateTime) As String
    2.         Dim Year, Month, Day As Integer
    3.  
    4.         ' Function to display difference between two dates in Years, Months and Days, calling routine ensures that DateOne is always smaller than DateTwo
    5.         If DateOne <> DateTwo Then                                          ' If both dates the same then exit with zeros returned otherwise a difference of one year gets returned!!!
    6.             ' Years
    7.             If DateTwo.Year > DateOne.Year Then                       ' If year is the same in both dates, an out of range exception is thrown!!!
    8.                 Year = DateTwo.AddYears(-DateOne.Year).Year       ' Subtract DateOne years from DateTwo years to get difference
    9.             End If
    10.  
    11.             ' Months
    12.             Month = DateTwo.AddMonths(-DateOne.Month).Month         ' Subtract DateOne months from DateTwo months
    13.             If DateTwo.Month <= DateOne.Month Then                        ' Decrement year by one if DateTwo months hasn't exceeded DateOne months, i.e. not full year yet
    14.                 If Year > 0 Then Year -= 1
    15.             End If
    16.  
    17.             ' Days
    18.             Day = DateTwo.AddDays(-DateOne.Day).Day                         ' Subtract DateOne days from DateTwo days
    19.             If DateTwo.Day <= DateOne.Day Then                             ' Decrement month by one if DateTwo days hasn't exceeded DateOne days - not full month yet
    20.                 If Month > 0 Then Month -= 1
    21.             End If
    22.             If DateOne.Day = DateTwo.Day Then                        ' Avoid silliness like "1 month 31 days" instead of 2 months
    23.                 Day = 0                                                                          ' Reset days
    24.                 Month += 1                                                                   ' And increment month
    25.             End If
    26.  
    27.             If Month = 12 Then                                                         ' Months value goes up to 12, and we want a maximum of 11, so:
    28.                 Month = 0                                                                     ' Reset months to zero
    29.                 Year += 1                                                                       ' And increment year
    30.             End If
    31.             If DateOne.Year = DateTwo.Year AndAlso DateOne.Month = DateOne.Month Then            ' If year and month are the same in DateOne & DateTwo then month = 12 and therefore year has been incremented
    32.                 Year = 0                                                                     ' So reset it
    33.             End If
    34.  
    35.         End If         ' DateOne <> DateTwo
    36.  
    37.         Return Year & " years, " & Month & " months, " & Day & " days"                  ' Concatenate string and return to calling routine
    38.  
    39.     End Function       ' Y_M_D_Diff
    Nick.

Posting Permissions

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



Click Here to Expand Forum to Full Width