![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| | |||||||
| | Vista - Date duration function that returns years, months, weeks and days? |
| |
| 06-29-2009 | #1 |
| | Date duration function that returns years, months, weeks and days? 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 |
| 06-29-2009 | #2 |
| | Re: Date duration function that returns years, months, weeks and days? "HAL07" <yahoohal@xxxxxx> wrote in message news:OOjg8bI%23JHA.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 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. |
| My System Specs |
| 06-29-2009 | #3 |
| | Re: Date duration function that returns years, months, weeks anddays? Pegasus [MVP] wrote: Quote: > "HAL07" <yahoohal@xxxxxx> wrote in message > news:OOjg8bI%23JHA.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 > 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. > > -- -- HAL07, Engineering Services, Norway |
| My System Specs |
| 06-29-2009 | #4 |
| | Re: Date duration function that returns years, months, weeks anddays? HAL07 schrieb: 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 > Dim dtNow : dtNow = #6/29/2009 01:02:03# Dim aBirth : aBirth = Array( #1/4/1980 13:42:00#, #7/13/1953 07:08:09#, dtNow, dtNow - 7, #2/29/2004 01:02:03# ) Dim dtBirth For Each dtBirth In aBirth DateDiffUnits dtNow, dtBirth WScript.Echo Next Function DateDiffUnits( dtNow, dtBirth ) ReDim aRVal( 6 ) Dim aUnits : aUnits = Array( "yyyy", "m", "ww", "d", "h", "n", "s" ) Dim dtCalc : dtCalc = dtBirth Dim nIdx, dtTmp WScript.Echo dtCalc For nIdx = 0 To UBound( aUnits ) aRVal( nIdx ) = DateDiff( aUnits( nIdx ), dtCalc, dtNow ) dtTmp = DateAdd( aUnits( nIdx ), aRVal( nIdx ), dtCalc ) If dtTmp > dtNow Then aRVal( nIdx ) = aRVal( nIdx ) - 1 dtCalc = DateAdd( aUnits( nIdx ), aRVal( nIdx ), dtCalc ) WScript.Echo dtCalc, Right( " " & aRVal( nIdx ), 2 ), aUnits( nIdx ) Next WScript.Echo dtNow If dtNow <> dtCalc Then WScript.Echo "Surprise", dtNow, dtCalc End If DateDiffUnits = aRVal End Function output: === OldAge: you are y m w h m s old ======= 04.01.1980 13:42:00 04.01.2009 13:42:00 29 yyyy 04.06.2009 13:42:00 5 m 25.06.2009 13:42:00 3 ww 28.06.2009 13:42:00 3 d 29.06.2009 00:42:00 11 h 29.06.2009 01:02:00 20 n 29.06.2009 01:02:03 3 s 29.06.2009 01:02:03 13.07.1953 07:08:09 13.07.2008 07:08:09 55 yyyy 13.06.2009 07:08:09 11 m 27.06.2009 07:08:09 2 ww 28.06.2009 07:08:09 1 d 29.06.2009 00:08:09 17 h 29.06.2009 01:01:09 53 n 29.06.2009 01:02:03 54 s 29.06.2009 01:02:03 29.06.2009 01:02:03 29.06.2009 01:02:03 0 yyyy 29.06.2009 01:02:03 0 m 29.06.2009 01:02:03 0 ww 29.06.2009 01:02:03 0 d 29.06.2009 01:02:03 0 h 29.06.2009 01:02:03 0 n 29.06.2009 01:02:03 0 s 29.06.2009 01:02:03 22.06.2009 01:02:03 22.06.2009 01:02:03 0 yyyy 22.06.2009 01:02:03 0 m 29.06.2009 01:02:03 1 ww 29.06.2009 01:02:03 0 d 29.06.2009 01:02:03 0 h 29.06.2009 01:02:03 0 n 29.06.2009 01:02:03 0 s 29.06.2009 01:02:03 29.02.2004 01:02:03 28.02.2009 01:02:03 5 yyyy 28.06.2009 01:02:03 4 m 28.06.2009 01:02:03 0 ww 29.06.2009 01:02:03 1 d 29.06.2009 01:02:03 0 h 29.06.2009 01:02:03 0 n 29.06.2009 01:02:03 0 s 29.06.2009 01:02:03 === OldAge: 0 done (00:00:00) ============== |
| My System Specs |
| 06-29-2009 | #5 |
| | Re: Date duration function that returns years, months, weeks and days? "Pegasus [MVP]" <news@xxxxxx> wrote in message news:%230Q5uiI%23JHA.2120@xxxxxx Quote: > > "HAL07" <yahoohal@xxxxxx> wrote in message > news:OOjg8bI%23JHA.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 > 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. 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. Option Explicit Dim Before, Elapsed Before = Now WScript.Sleep 5000 Elapsed = Now - Before MsgBox CDate(Elapsed) & vbCrLf & FormatNumber(Elapsed, 15) To get a one second resolution, the OP would have to enter the birth time to the second, subtract Now, and add/subtract a number representing the difference in time zone between birth and the current location. The whole number part would be age in days and the fractional part would represent the additional fraction of a day. I think getting years and months might be relatively easy to get, but weeks would require some extra math and a little more math to get days, which the OP didn't ask for but may well be desired, and a little more math to convert the remainder to hours and nutes if desired, and seconds. Dr. John Stockton's web site or a link from it may provide a script that almost meets the OPs requirements. His signature typically contains links like: (c) John Stockton, nr London, UK. ?...@xxxxxx Turnpike v6.05. Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc. -Paul Randall |
| My System Specs |
| 06-29-2009 | #6 |
| | Re: Date duration function that returns years, months, weeks and days? "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 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 >> 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. 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. |
| My System Specs |
| 06-29-2009 | #7 |
| | Re: Date duration function that returns years, months, weeks anddays? Paul Randall schrieb: [...] Quote: > I always thought that the DateDif function returned a double precision > floating point number [...] The DateDiff function "Returns the number of intervals between two dates." The subtype of the return value is Long. The interval/unit depends on the first parameter. |
| My System Specs |
| 06-29-2009 | #8 |
| | Re: Date duration function that returns years, months, weeks and days? "HAL07" <yahoohal@xxxxxx> wrote in message news:%23fAtxHL%23JHA.4648@xxxxxx Quote: > thanks for helping me write one, however I asked if anybody had such a > function ready. > -- > -- HAL07, Engineering Services, Norway respondent will agree to spend his own time and deliver it to you on a platter but he/she would, of course, need a tight specification about the ambiguous "month" requirement. |
| My System Specs |
| 06-29-2009 | #9 |
| | Re: Date duration function that returns years, months, weeks and days? 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. 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 |
| 06-29-2009 | #10 |
| | Re: Date duration function that returns years, months, weeks and days? Pegasus [MVP] wrote: Quote: > > "HAL07" <yahoohal@xxxxxx> wrote in message > news:%23fAtxHL%23JHA.4648@xxxxxx > Quote: > > thanks for helping me write one, however I asked if anybody had such a > > function ready. > > -- > > -- HAL07, Engineering Services, Norway > You might have to do some of the work yourself. It's quite basic. Maybe Quote: > respondent will agree to spend his own time and deliver it to you on a > platter but he/she would, of course, need a tight specification about the > ambiguous "month" requirement. tells how many ymd a person lived. I leave the hours, minutes and seconds as an exercise for OP to work out. HTH. 'GetAge_in_YMDs.vbs 'Displays number of years, months and days elapsed since a specified date. On Error Resume Next DOB = CDate(InputBox("Enter your Birth date:", "Birthday calculator")) If Err.Number > 0 Then Wscript.Quit On Error Goto 0 'Displays the number of years, months and days between two dates by 'adding a second InputBox for DOD, default is todays date. DOD = Date Wscript.Echo GetAge(DOB, DOD) Function GetAge(DOB, DOD) 'Ensure the DOB is LESS than DOD If DOB > DOD Then MsgBox "Begin date is greater than end date.", _ vbOKOnly + vbInformation, "Unacceptable Date" Wscript.Quit End If 'Get the years between the two dates yrs = DateDiff("yyyy", DOB, DOD) yrs = yrs - Abs(DateAdd("yyyy", yrs, DOB) > DOD) 'Get the months between the two dates that exceed the years mos = DateDiff("m", DOB, DOD) mos = mos - Abs(DateAdd("m", mos, DOB) > DOD) - (yrs * 12) 'Get the number of days between the two dates that exceed the years + months ... dys = DateDiff("n", DateAdd("m", mos + yrs * 12, DOB), DOD) \ 1440 'Build strings If yrs = 1 Then yrs = yrs & " year, " Else yrs = yrs & " years, " End If If mos = 1 Then mos = mos & " month, " Else mos = mos & " months, " End If If dys = 1 Then dys = dys & " day." Else dys = dys & " days." End If 'Return the string GetAge = yrs & mos & dys End Function -- Todd Vargo (Post questions to group only. Remove "z" to email personal messages) |
| My System Specs |
![]() |
| Thread Tools | |
| |
| Similar Threads for: Date duration function that returns years, months, weeks and days? | ||||
| Thread | Forum | |||
| BSoD every few days now (past 2 weeks) | General Discussion | |||
| 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 | |||