![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #11 (permalink) |
| | Re: Date duration function that returns years, months, weeks and days? "HAL07" <yahoohal@xxxxxx> wrote Quote: > thanks for helping me write one, however I asked if anybody had such a function ready. LFS Public Sub DateIntervals(ByVal Date1 As Date, ByVal Date2 As Date, ParamArray Prams()) Dim swap As Date, test As Date Dim i As Long, itr As String Const interval = "mdhns" ' Returns the greatest full interval (yr, mo, day, hr, min, sec) between two dates ' Calling procedure supplies variable(s) for the desired interval(s) ' Ex1: DateIntervals Birthday, Now, Yr, Mo, Dy, Hr, Mn, Sec ' (Exactly how old are you?) ' Ex2: DateIntervals Now, "25 Dec", , , Days ' (How many days until Christmas?) If UBound(Prams) < 0 Then Exit Sub If (DateValue(Date1) = 0) Xor (DateValue(Date2) = 0) Then ' Assume today if one is a time and the other is a date... If DateValue(Date1) = 0 Then Date1 = Date1 + DateValue(Now) If DateValue(Date2) = 0 Then Date2 = Date2 + DateValue(Now) End If If Date1 > Date2 Then ' Swap dates if first is after second... swap = Date1 Date1 = Date2 Date2 = swap End If If Not IsMissing(Prams(0)) Then ' Adjust year values Prams(0) = Year(Date2) - Year(Date1) test = DateAdd("yyyy", Prams(0), Date1) Prams(0) = Prams(0) + (test > Date2) Date1 = DateAdd("yyyy", Prams(0), Date1) If UBound(Prams) < 1 Then Exit Sub End If For i = 1 To IIf(UBound(Prams) > 5, 5, UBound(Prams)) ' Adjust remaining values If Not IsMissing(Prams(i)) Then itr = Mid$(interval, i, 1) Prams(i) = DateDiff(itr, Date1, Date2) Prams(i) = Prams(i) + (DateAdd(itr, Prams(i), Date1) > Date2) Date1 = DateAdd(itr, Prams(i), Date1) End If Next i End Sub |
My System Specs![]() |
| | #12 (permalink) |
| | Re: Date duration function that returns years, months, weeks and days? Larry Serflaten wrote: Quote: > "HAL07" <yahoohal@xxxxxx> wrote > > Quote: >> thanks for helping me write one, however I asked if anybody had such >> a function ready. > > > Public Sub DateIntervals(ByVal Date1 As Date, ByVal Date2 As Date, > ParamArray Prams()) > Dim swap As Date, test As Date the obvious flaw of assigning datatypes to the arguments and variables (all variables are Variant in vbscript so the "As Date" is not allowed), there is no ParamArray keyword in vbscript; so HALO7, you will need to rewrite this to get around those limitations. -- HTH, Bob Barrows |
My System Specs![]() |
| | #13 (permalink) |
| | Re: Date duration function that returns years, months, weeks and days? Larry Serflaten wrote: Quote: > "HAL07" <yahoohal@xxxxxx> wrote > > Quote: >> thanks for helping me write one, however I asked if anybody had such >> a function ready. > See if this helps.... > > LFS > > > Public Sub DateIntervals(ByVal Date1 As Date, ByVal Date2 As Date, 'initialize all the argument variables keeping in mind that there are no optional arguments in vbscript procedures. Birthday=#5/18/1975 09:00# Yr=0 ' 0 or greater says "calculate this value" Mo=0 Dy=0 Wk=0 Hr=0 Mn=-1 ' -1 says "don't calculate this value" Sec=0 DateIntervals Birthday, Now, Yr, Mo,Wk, Dy, Hr, Mn, Sec msgbox "You are " & Yr & " years, " & Mo & " months, " & _ Wk & " weeks, " & Dy & " days, " & Hr & " hours and " & _ Sec & " seconds old" Public Sub DateIntervals(ByVal Date1, ByVal Date2, Yr, _ Mo,Wk, Dy, Hr, Mn, Sec) Dim swap, test Dim i, itr Const interval = "ymwdhns" ' Returns the greatest full interval (yr, mo, day, hr, min, sec) between two dates ' Calling procedure supplies variable(s) for the desired interval(s) Prams=Array(Yr, Mo, Wk,Dy, Hr, Mn, Sec) If (DateValue(Date1) = 0) Xor (DateValue(Date2) = 0) Then ' Assume today if one is a time and the other is a date... If DateValue(Date1) = 0 Then Date1 = Date1 + DateValue(Now) If DateValue(Date2) = 0 Then Date2 = Date2 + DateValue(Now) End If If Date1 > Date2 Then ' Swap dates if first is after second... swap = Date1 Date1 = Date2 Date2 = swap End If For i = 0 To UBound(Prams) If Prams(i) > -1 Then itr = Mid(interval, i+1, 1) itr=replace(replace(itr,"w","ww"),"y","yyyy") ' itr=replace(itr,"y","yyyy") Prams(i) = DateDiff(itr, Date1, Date2) Prams(i) = Prams(i) + (DateAdd(itr, Prams(i), Date1) > Date2) Date1 = DateAdd(itr, Prams(i), Date1) End If Next 'i Yr=Prams(0) Mo=Prams(1) Wk=Prams(2) Dy=Prams(3) Hr=Prams(4) Mn=Prams(5) Sec=Prams(6) End Sub -- HTH, Bob Barrows |
My System Specs![]() |
| | #14 (permalink) |
| | Re: Date duration function that returns years, months, weeks and days? "Bob Barrows" <reb01501@xxxxxx> wrote Quote: Quote: > > See if this helps.... > OK, I was bored so I created a vbscript version of this: Good job! LFS |
My System Specs![]() |
| | #15 (permalink) |
| | Re: Date duration function that returns years, months, weeks and d I'll say it too, nice job on the function! -- Jeff C Live Well .. Be Happy In All You Do "Bob Barrows" wrote: Quote: > Paul Randall wrote: Quote: Quote: > >> > >> Use the DateDiff function to determine the difference (in seconds) > >> between the two dates, then convert the seconds back into years, > >> months, days etc. Note that your desired output ("You are 29 years, > >> x months") is ambiguous: Are your months 28, 29, 30 or 31 days long? > >> > >> If unsure how to use the DateDiff function then I recommend that you > >> download the help file script56.chm from the Microsoft site. > > I always thought that the DateDif function returned a double precision > > floating point number, whose whole number part is the number of days > > since the beginning of time for the current operating system and > > whose fractional part is the fraction of the current day that has > > elapsed, and that the resolution is about one eighteenth of a second. > No, that sounds more like the definition of the Date datatype (instead > of "beginning of time", use "seed date" which for vb/vba/vbscript is > 1899-12-30). From the documentation, the DateDiff function returns an > integer representing "... the number of intervals between two dates. " > > This displays "Long": > msgbox typename(datediff("d",#2009-06-01#, date)) > > -- > HTH, > Bob Barrows > > > |
My System Specs![]() |
| | #16 (permalink) |
| | Re: Date duration function that returns years, months, weeks and days? "Pegasus [MVP]" <news@xxxxxx> wrote in message news:%23UgHkGM%23JHA.4900@xxxxxx Quote: > > "Paul Randall" <paulr901@xxxxxx> wrote in message > news:em%23izwL%23JHA.4648@xxxxxx Quote: >> >> "Pegasus [MVP]" <news@xxxxxx> wrote in message >> news:%230Q5uiI%23JHA.2120@xxxxxx Quote: >>> >>> "HAL07" <yahoohal@xxxxxx> wrote in message >>> news:OOjg8bI%23JHA.4692@xxxxxx >>>> Hi there, do anybody by chance have such a function laying around? >>>> >>>> I would like to e.g. output wscript.echo "You are " & >>>> dateduration(now(), "1980.01.04", "13:42" ) & " old." >>>> >>>> Will output: >>>> You are 29 years, x months, x weeks, x hours and x seconds old >>>> >>>> >>>> >>>> -- >>>> -- HAL07, Engineering Services, Norway >>>> -- Info: social.technet.microsoft.com/Forums/ replaces a lot of the >>>> newsgroups >>> >>> Use the DateDiff function to determine the difference (in seconds) >>> between the two dates, then convert the seconds back into years, months, >>> days etc. Note that your desired output ("You are 29 years, x months") >>> is ambiguous: Are your months 28, 29, 30 or 31 days long? >>> >>> If unsure how to use the DateDiff function then I recommend that you >>> download the help file script56.chm from the Microsoft site. >> I always thought that the DateDif function returned a double precision >> floating point number, whose whole number part is the number of days >> since the beginning of time for the current operating system and whose >> fractional part is the fraction of the current day that has elapsed, and >> that the resolution is about one eighteenth of a second. > According to script56.chm, DateDiff returns whatever you want it to > return: Years, quarters, months, days etc. There are 10 different > "intervals" you can choose from. Double precision floating point number is > not one of them. The following is a direct quote from the help file: > ============= > The following example uses the DateDiff function to display the number of > days between a given date and today: > > Function DiffADate(theDate) > DiffADate = "Days from today: " & DateDiff("d", Now, theDate) > End Function > ============= > Note the "d" interval specifier in the DateDiff function. to subtracting two date type variables, when in reality, they are two different things. Thanks for straightening me out. -Paul Randall |
My System Specs![]() |
| | #17 (permalink) |
| | Re: Date duration function that returns years, months, weeks and days? Hal, be aware that doing this to the second is going to give you bogus results. For example, given the 1980 birthdate to the second, strict calculations will now be off by 16 seconds due to insertions of leap seconds (which by definition, will not be accounted for in duration calculations using the straightforward differences of two dates/times): http://tycho.usno.navy.mil/leapsec.html However, this is still arguably _legitimate_ even for precise usage since circadian cycle is a significant part of the meaning of measures like this. "HAL07" <yahoohal@xxxxxx> wrote in message news:OOjg8bI#JHA.4692@xxxxxx Quote: > Hi there, do anybody by chance have such a function laying around? > > I would like to e.g. output wscript.echo "You are " & > dateduration(now(), "1980.01.04", "13:42" ) & " old." > > Will output: > You are 29 years, x months, x weeks, x hours and x seconds old > > > > -- > -- HAL07, Engineering Services, Norway > -- Info: social.technet.microsoft.com/Forums/ replaces a lot of the > newsgroups |
My System Specs![]() |
| | #18 (permalink) |
| | Re: Date duration function that returns years, months, weeks anddays? Bob Barrows wrote: Quote: > Larry Serflaten wrote: Quote: >> "HAL07" <yahoohal@xxxxxx> wrote >> >> Quote: >>> thanks for helping me write one, however I asked if anybody had such >>> a function ready. >> >> LFS >> >> >> Public Sub DateIntervals(ByVal Date1 As Date, ByVal Date2 As Date, > OK, I was bored so I created a vbscript version of this: > > 'initialize all the argument variables keeping in mind that there are no > optional arguments in vbscript procedures. > Birthday=#5/18/1975 09:00# > Yr=0 ' 0 or greater says "calculate this value" > Mo=0 > Dy=0 > Wk=0 > Hr=0 > Mn=-1 ' -1 says "don't calculate this value" > Sec=0 > DateIntervals Birthday, Now, Yr, Mo,Wk, Dy, Hr, Mn, Sec > msgbox "You are " & Yr & " years, " & Mo & " months, " & _ > Wk & " weeks, " & Dy & " days, " & Hr & " hours and " & _ > Sec & " seconds old" > > Public Sub DateIntervals(ByVal Date1, ByVal Date2, Yr, _ > Mo,Wk, Dy, Hr, Mn, Sec) > Dim swap, test > Dim i, itr > Const interval = "ymwdhns" > ' Returns the greatest full interval (yr, mo, day, hr, min, sec) between > two dates > ' Calling procedure supplies variable(s) for the desired interval(s) > > Prams=Array(Yr, Mo, Wk,Dy, Hr, Mn, Sec) > > If (DateValue(Date1) = 0) Xor (DateValue(Date2) = 0) Then > ' Assume today if one is a time and the other is a date... > If DateValue(Date1) = 0 Then Date1 = Date1 + DateValue(Now) > If DateValue(Date2) = 0 Then Date2 = Date2 + DateValue(Now) > End If > > If Date1 > Date2 Then > ' Swap dates if first is after second... > swap = Date1 > Date1 = Date2 > Date2 = swap > End If > > For i = 0 To UBound(Prams) > If Prams(i) > -1 Then > itr = Mid(interval, i+1, 1) > itr=replace(replace(itr,"w","ww"),"y","yyyy") > ' itr=replace(itr,"y","yyyy") > Prams(i) = DateDiff(itr, Date1, Date2) > Prams(i) = Prams(i) + (DateAdd(itr, Prams(i), Date1) > Date2) > Date1 = DateAdd(itr, Prams(i), Date1) > End If > Next 'i > Yr=Prams(0) > Mo=Prams(1) > Wk=Prams(2) > Dy=Prams(3) > Hr=Prams(4) > Mn=Prams(5) > Sec=Prams(6) > > End Sub > Thank you very much Larry ![]() -- -- HAL07, Engineering Services, Norway |
My System Specs![]() |
| | #19 (permalink) |
| | Re: Date duration function that returns years, months, weeks anddays? On Jun 30, 3:44*am, "Alex K. Angelopoulos" <alex(dot) k(dot again) angelopoulos(at)gmail.com> wrote: Quote: > However, this is still arguably _legitimate_ even for precise usage since > circadian cycle is a significant part of the meaning of measures like this. http://www.ucolick.org/~sla/leapsecs/epochtime.html It is beyond the scope of most software to make the policy decision that international governments have not done in a consistent fashion. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Re: Microsoft extends XP downgrade rights date by six months | Vista General | |||
| Re: Microsoft extends XP downgrade rights date by six months | Vista General | |||
| Re: Microsoft extends XP downgrade rights date by six months | Vista General | |||
| Re: Microsoft extends XP downgrade rights date by six months | Vista General | |||
| Date difference in days and months | .NET General | |||