Trying to get a Date Breakdown

  • Thread starter Thread starter Atley
  • Start date Start date
A

Atley

I am trying to get a overall difference on two dates,

I can get the difference in Years, Months, Weeks, Days, Hours, Minutes,
Seconds, no problems...

What I cannot seem to figure out is how to get:

2 Years, 3 Months, 1 Week, 3 Days, 14 Hours, 21 Minutes, 33 Seconds.

The hardest part of that, the part I cannot figure out, is the Month
section... all the others are static, defined length components, but Months
change, are all different...

Help! anyone know how you do this? I am stuck...

Thanks So Much for your Help!

Atley
 
what about getting the difference incerementally.

1st you get the difference in Year and substract it
2nd you get the difference in month and substract it
3rd .... (etc ...)
 
Yeah, I thought of that, but how do I decide what to delete from the total,
based on how many months?
 
I don't get you.

wether month have different lenght or you modify start or end date, the
result should be the same (I guess)

like
2 Feb (2) 2002 - 1st Jan (1) 2001
= 1Y + (2 Feb 2001 - 1st Jan 2001) OR 1 year + (2 Feb 2002 - 1st Jan 2002)
= 1Y + 1M + (2 Jan 2001 - 1st Jan 2001) OR 1Y+1M+(2Feb2002-1stFeb2002)
= 1Y + 1M + 1D OR 1Y + 1M + 1D

isn't it ?
 
Yea, but how would you do that in code, that will work for any dates put in?
What I am saying is I can't find the constructs or methods that will do this
effectively.
 
I'm not sure I understand exactly what the problem is, but isn't it just
something as simple as:

Dim d1 As New Date(2006, 10, 20)
Dim d2 As New Date(2003, 1, 9)
Dim yDiff As Integer = d1.Year - d2.Year
Dim mDiff As Integer = d1.Month - d2.Month
Dim dDiff As Integer = d1.Day - d2.Day

Or are you asking for something more complicated?

Steven
 
something like this?

dim date1 as datetime
dim date2 as datetime

dim ticks1 as long = date1.ticks
dim ticks2 as long = date2.ticks

dim difference as new timespan(math.abs(ticks1 - ticks2))


Dominique
 
Thanks, but i can get the DateDiff several different ways, the problem is
with the breakdown into how much time is left as a rundown ie 1 year, 2
months, 1 week, 3 days, 6 hours, 23 minutes, 41 seconds...
 
Hi Atley,

Did you look at the
timespan.tickspersecond
timespan.ticksperyear
timespan.ticksperhour
etc.

I hope this helps,

Cor
 
aha
you can do somthing like this...

Dim dt As New DateTime(math.abs(ticks1 - ticks2))
Dim msg As String = "Differnce is:" & vbCrLf & _
"years: " & (dt.Year - 1) & vbCrLf & _
"months: " & (dt.Month - 1) & vbCrLf & _
"days: " & (dt.Day - 1) & vbCrLf & _
"hours: " & dt.Hour & vbCrLf & _
"minutes: " & dt.Minute & vbCrLf & _
"seconds: " & dt.Second

doing -1 for year, month and day:
new datetime(ticks as long) -> gives you a date, not a timespan
and the date is calculated as the number of ticks since 01-01-0001 00:00:00
so doing -1 for day, month and year gives you the remaining time for each
datetime part

isn't that kewl :-)


something not so kewl
try this
date1 = now
date2 = date1.addYear(1)

result everything 0 except for year... and day (also 1)

tried a little with other "year-add's"
90 -> year=90, day=1
99 -> year=99, day=0

wellwell, bug for small number of add years only?
or am I missing something?
 
Dominique,
You know you can shorten that to:
dim date1 as datetime
dim date2 as datetime

dim difference as timespan = date2.Subtract(date1)

Which assumes that date2 is after date1.

Hope this helps
Jay
 
Dominique,
How about
Dim difference as TimeSpan = date2.Subtract(date1)
difference = difference.Duration() ' correct to positive offset
Now years is difference.Days/365 (ignoring leap year effects 365
days/year)
Days = difference.Days Mod 365
hours, minutes and seconds are directly readable from the timespan.

Ron Allen
 
Ron,

Yea that works, and that is pretty much what I was doing, except you have
the exact same problem with months that I did.
 
well I used the dateTime and not the TimeSpan just to have an easy way to
find the years, months and days...
 
Back
Top