all groups > vb.net > february 2004 >
You're in the

vb.net

group:

Trying to get a Date Breakdown


Trying to get a Date Breakdown Atley
2/26/2004 10:30:10 PM
vb.net:
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

Re: Trying to get a Date Breakdown Atley
2/26/2004 11:52:57 PM
Yeah, I thought of that, but how do I decide what to delete from the total,
based on how many months?





[quoted text, click to view]

Re: Trying to get a Date Breakdown Atley
2/27/2004 1:30:42 AM
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.




[quoted text, click to view]

Re: Trying to get a Date Breakdown Steven Licciardi
2/27/2004 7:31:21 AM
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

[quoted text, click to view]

Re: Trying to get a Date Breakdown Atley
2/27/2004 7:39:24 AM
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...



"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:OK5fkhS$DHA.552@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

Re: Trying to get a Date Breakdown Jay B. Harlow [MVP - Outlook]
2/27/2004 8:16:15 AM
Dominique,
You know you can shorten that to:

[quoted text, click to view]

Which assumes that date2 is after date1.

Hope this helps
Jay

"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:OK5fkhS$DHA.552@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

Re: Trying to get a Date Breakdown Ron Allen
2/27/2004 9:33:15 AM
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
[quoted text, click to view]

Re: Trying to get a Date Breakdown Cor
2/27/2004 9:53:05 AM
Hi Atley,

Did you know that Date is the topic of this week on MSDN

http://msdn.microsoft.com/netframework/default.aspx?pull=/library/en-us/dndotnet/html/datetimecode.asp

Cor

Re: Trying to get a Date Breakdown Atley
2/27/2004 10:53:09 AM
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.


[quoted text, click to view]

Re: Trying to get a Date Breakdown Atley
2/27/2004 10:54:22 AM
Sweet...

It works like a charm... you are the best! thanks for the help.




"Dominique Vandensteen" <domi.vds_insert@tralala_tenforce.com> wrote in
message news:uSvUEgT$DHA.552@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

Re: Trying to get a Date Breakdown Dominique Vandensteen
2/27/2004 12:54:03 PM
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




[quoted text, click to view]

Re: Trying to get a Date Breakdown Cor
2/27/2004 1:59:38 PM
Hi Atley,

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

I hope this helps,

Cor

Re: Trying to get a Date Breakdown Dominique Vandensteen
2/27/2004 2:45:55 PM
aha
you can do somthing like this...


[quoted text, click to view]
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?





[quoted text, click to view]

Re: Trying to get a Date Breakdown Lloyd Dupont
2/27/2004 3:33:03 PM
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 ...)

[quoted text, click to view]

Re: Trying to get a Date Breakdown Peter Seaman
2/27/2004 3:55:33 PM
What is a year? What is a month?

Peter Seaman

Re: Trying to get a Date Breakdown Lloyd Dupont
2/27/2004 4:46:12 PM
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 ?

[quoted text, click to view]

Re: Trying to get a Date Breakdown Dominique Vandensteen
2/27/2004 7:24:19 PM
well I used the dateTime and not the TimeSpan just to have an easy way to
find the years, months and days...


[quoted text, click to view]

AddThis Social Bookmark Button