Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Date duration function that returns years, months, weeks and days?

Reply
 
Old 06-29-2009   #11 (permalink)
Larry Serflaten


 
 

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.
See if this helps....

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 SpecsSystem Spec
Old 06-29-2009   #12 (permalink)
Bob Barrows


 
 

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
This is a VB/VBA function that will not work in vbscript - along with
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 SpecsSystem Spec
Old 06-29-2009   #13 (permalink)
Bob Barrows


 
 

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,
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

--
HTH,
Bob Barrows


My System SpecsSystem Spec
Old 06-29-2009   #14 (permalink)
Larry Serflaten


 
 

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:
With a little help from friends, he got something useable....

Good job!
LFS


My System SpecsSystem Spec
Old 06-29-2009   #15 (permalink)
Jeff C


 
 

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 SpecsSystem Spec
Old 06-29-2009   #16 (permalink)
Paul Randall


 
 

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.
You are right. I was wrong. In my slightly warped mind, DateDiff equates
to subtracting two date type variables, when in reality, they are two
different things. Thanks for straightening me out.

-Paul Randall


My System SpecsSystem Spec
Old 06-30-2009   #17 (permalink)
Alex K. Angelopoulos


 
 

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 SpecsSystem Spec
Old 07-01-2009   #18 (permalink)
HAL07


 
 

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.
>> See if this helps....
>>
>> 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 SpecsSystem Spec
Old 07-04-2009   #19 (permalink)
Steve Allen


 
 

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.
The number of elapsed seconds depends upon the jurisdiction
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46