|
-
Mar 3rd, 2004, 06:55 PM
#1
Thread Starter
PowerPoster
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:
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.
-
Mar 3rd, 2004, 07:12 PM
#2
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]
-
Mar 3rd, 2004, 08:56 PM
#3
Addicted Member
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
-
Mar 4th, 2004, 12:40 PM
#4
Thread Starter
PowerPoster
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?
-
Mar 4th, 2004, 03:05 PM
#5
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]
-
Mar 4th, 2004, 03:22 PM
#6
Thread Starter
PowerPoster
Excellent, thank you kindly. I'll send you a few hours of virtual sleep by way of thanks
-
Mar 5th, 2004, 03:52 PM
#7
Thread Starter
PowerPoster
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:
Private Function Y_M_D_Diff(ByVal DateOne As DateTime, ByVal DateTwo As DateTime) As String
Dim Year, Month, Day As Integer
' Function to display difference between two dates in Years, Months and Days, calling routine ensures that DateOne is always smaller than DateTwo
If DateOne <> DateTwo Then ' If both dates the same then exit with zeros returned otherwise a difference of one year gets returned!!!
' Years
If DateTwo.Year > DateOne.Year Then ' If year is the same in both dates, an out of range exception is thrown!!!
Year = DateTwo.AddYears(-DateOne.Year).Year ' Subtract DateOne years from DateTwo years to get difference
End If
' Months
Month = DateTwo.AddMonths(-DateOne.Month).Month ' Subtract DateOne months from DateTwo months
If DateTwo.Month <= DateOne.Month Then ' Decrement year by one if DateTwo months hasn't exceeded DateOne months, i.e. not full year yet
If Year > 0 Then Year -= 1
End If
' Days
Day = DateTwo.AddDays(-DateOne.Day).Day ' Subtract DateOne days from DateTwo days
If DateTwo.Day <= DateOne.Day Then ' Decrement month by one if DateTwo days hasn't exceeded DateOne days - not full month yet
If Month > 0 Then Month -= 1
End If
If DateOne.Day = DateTwo.Day Then ' Avoid silliness like "1 month 31 days" instead of 2 months
Day = 0 ' Reset days
Month += 1 ' And increment month
End If
If Month = 12 Then ' Months value goes up to 12, and we want a maximum of 11, so:
Month = 0 ' Reset months to zero
Year += 1 ' And increment year
End If
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
Year = 0 ' So reset it
End If
End If ' DateOne <> DateTwo
Return Year & " years, " & Month & " months, " & Day & " days" ' Concatenate string and return to calling routine
End Function ' Y_M_D_Diff
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|