Hello,

I'm trying to create a script that tells the difference between 2 dates
in the following format:

5 years, 4 months, 1 Week, 3 days, 7 hours, 3 minutes and 34 seconds.

This is what I have so far:

var1 = dateserial(2005, 01, 25)
var3 = now()
var3 = Datediff("s", var1, now_var)

but this yealds an answer only in seconds. I'm not sure where to go
from here any help is appricatied.

Re: DateDiff with special format. by Walter

Walter
Wed Aug 16 13:13:03 CDT 2006

<Cryptographic_ICE@yahoo.com> wrote in message
news:1155733658.556806.101030@b28g2000cwb.googlegroups.com...
: Hello,
:
: I'm trying to create a script that tells the difference between 2 dates
: in the following format:
:
: 5 years, 4 months, 1 Week, 3 days, 7 hours, 3 minutes and 34 seconds.
:
: This is what I have so far:
:
: var1 = dateserial(2005, 01, 25)
: var3 = now()
: var3 = Datediff("s", var1, now_var)
:
: but this yealds an answer only in seconds. I'm not sure where to go
: from here any help is appricatied.
:

Function GetDateDiff(Date1,Date2)
Dim Diff
Diff = CDate(abs(Date1 - Date2) + 2)
GetDateDiff = Array(Year(Diff) - 1900,Month(Diff) - 1,Day(Diff) \ 7,_
Day(Diff) mod
7,Hour(Diff),Minute(Diff),Second(Diff))
End Function

You have to pass two valid dates of type Date to the function. Its return
value will be an Array containing 7 numbers. The 7 numbers will be (in
order) the difference in years, months, weeks, days, hours, minutes and
seconds.



Re: DateDiff with special format. by Walter

Walter
Wed Aug 16 13:21:22 CDT 2006

Here's the function again without the word-wrapping.

Function GetDateDiff(Date1,Date2)
Dim Diff
Diff = CDate(abs(Date1 - Date2) + 2)
GetDateDiff = Array(Year(Diff) - 1900,Month(Diff) - 1,Day(Diff) \ 7,_
Day(Diff) mod 7,Hour(Diff),Minute(Diff),Second(Diff))
End Function

"Walter Zackery" <please_respond_to@group.com> wrote in message
news:ukh$d%23VwGHA.888@TK2MSFTNGP02.phx.gbl...
: <Cryptographic_ICE@yahoo.com> wrote in message
: news:1155733658.556806.101030@b28g2000cwb.googlegroups.com...
: Function GetDateDiff(Date1,Date2)
: Dim Diff
: Diff = CDate(abs(Date1 - Date2) + 2)
: GetDateDiff = Array(Year(Diff) - 1900,Month(Diff) - 1,Day(Diff) \ 7,_
: Day(Diff) mod
: 7,Hour(Diff),Minute(Diff),Second(Diff))
: End Function
:
: You have to pass two valid dates of type Date to the function. Its return
: value will be an Array containing 7 numbers. The 7 numbers will be (in
: order) the difference in years, months, weeks, days, hours, minutes and
: seconds.
:
:






Re: DateDiff with special format. by Dr

Dr
Wed Aug 16 16:48:10 CDT 2006

JRS: In article <1155733658.556806.101030@b28g2000cwb.googlegroups.com>
, dated Wed, 16 Aug 2006 06:07:38 remote, seen in news:microsoft.public.
scripting.vbscript, Cryptographic_ICE@yahoo.com posted :

>I'm trying to create a script that tells the difference between 2 dates
>in the following format:
>
>5 years, 4 months, 1 Week, 3 days, 7 hours, 3 minutes and 34 seconds.
>
>This is what I have so far:
>
>var1 = dateserial(2005, 01, 25)
>var3 = now()
>var3 = Datediff("s", var1, now_var)
>
>but this yealds an answer only in seconds. I'm not sure where to go
>from here any help is appricatied.

Since the number of hours in a day and the number of days in a month are
not constant, the answer is not well-determined. There's discussion in
my javascript date pages, I think.

Actually, VBS ignores day-length changes, but you'll still need to deal
with month-length.

You can easily get, from the answer in seconds, a meaningful result for
days hours minutes seconds by using the mod & div operations, as you
would do with pencil and paper.

YSCIB.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Re: DateDiff with special format. by Dr

Dr
Thu Aug 17 07:04:43 CDT 2006

JRS: In article <ukh$d#VwGHA.888@TK2MSFTNGP02.phx.gbl>, dated Wed, 16
Aug 2006 14:13:03 remote, seen in news:microsoft.public.scripting.vbscri
pt, Walter Zackery <please_respond_to@group.com> posted :
><Cryptographic_ICE@yahoo.com> wrote in message
>news:1155733658.556806.101030@b28g2000cwb.googlegroups.com...

>: I'm trying to create a script that tells the difference between 2 dates
>: in the following format:
>:
>: 5 years, 4 months, 1 Week, 3 days, 7 hours, 3 minutes and 34 seconds.


>Function GetDateDiff(Date1,Date2)
> Dim Diff
> Diff = CDate(abs(Date1 - Date2) + 2)
> GetDateDiff = Array(Year(Diff) - 1900,Month(Diff) - 1,Day(Diff) \ 7,_
> Day(Diff) mod
>7,Hour(Diff),Minute(Diff),Second(Diff))
>End Function
>
>You have to pass two valid dates of type Date to the function. Its return
>value will be an Array containing 7 numbers. The 7 numbers will be (in
>order) the difference in years, months, weeks, days, hours, minutes and
>seconds.

If the dates are equal, that gives me a difference of one day. ISTM
that you forgot to subtract 1 from Day(Diff).

If the difference is 60 days it gives 2 months 2 days; but 60 days from
March 1st is April 30th, so 1 month 4 weeks 1 day would be expected for
that.

If the difference exceeds about 8100 years, I think it will fail!

While the question is fundamentally unsatisfactory, better methods than
yours can be found (e.g. in my javascript date pages).

Actually, neither the IP nor you defined the length of a week.
Traditionally, and also by ISO 8601, it is always 7 days Gregorian; but
in odd places such as the USA the numbering of weeks is such that the
first and last week of a year can be shorter.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
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 moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Re: DateDiff with special format. by Walter

Walter
Thu Aug 17 14:15:10 CDT 2006


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:uMl3PPDbtF5EFwWZ@merlyn.demon.co.uk...
: If the dates are equal, that gives me a difference of one day. ISTM
: that you forgot to subtract 1 from Day(Diff).
:

You're absolutely right. The function should have been this:

Function GetDateDiff(Date1,Date2)
Dim Diff
Diff = CDate(abs(Date1 - Date2) + 2)
GetDateDiff = Array(Year(Diff) - 1900,Month(Diff) - 1,(Day(Diff) - 1) \
7,_
(Day(Diff) - 1) mod
7,Hour(Diff),Minute(Diff),Second(Diff))
End Function

: If the difference is 60 days it gives 2 months 2 days; but 60 days from
: March 1st is April 30th, so 1 month 4 weeks 1 day would be expected for
: that.
:

The fundamental problem is that the length of a year, month and week can all
vary. So depending on your starting point 60 days can be 2 months and 1 day,
2 months, 1 month and 30 days or 1 month and 29 days. The question is, do
you want the same interval of elapsed time to always return the same result?
Or should it depend on the years and months that you're starting with. I
chose the former approach. You probably chose the latter approach. I don't
see either method as being inherently superior; it depends on what you're
trying to achieve.




Re: DateDiff with special format. by Dr

Dr
Fri Aug 18 06:49:26 CDT 2006

JRS: In article <uniq2FjwGHA.4280@TK2MSFTNGP04.phx.gbl>, dated Thu, 17
Aug 2006 15:15:10 remote, seen in news:microsoft.public.scripting.vbscri
pt, Walter Zackery <please_respond_to@group.com> posted :
>
>The fundamental problem is that the length of a year, month and week can all
>vary.

Your code uses a fixed length for the week. That's fine for those
following ISO 8601 or religious tradition; but in places such as the USA
the customary numberings of weeks imply variable length...

> So depending on your starting point 60 days can be 2 months and 1 day,
>2 months, 1 month and 30 days or 1 month and 29 days.
Of course.

> The question is, do
>you want the same interval of elapsed time to always return the same result?

Unfortunately, the ordinary people, including management, will want that
and will also want the interval between a date and the same date in the
next month or year to be an exact number of months or years. They
cannot have both.

>Or should it depend on the years and months that you're starting with. I
>chose the former approach. You probably chose the latter approach. I don't
>see either method as being inherently superior; it depends on what you're
>trying to achieve.

Those who ask for such code must be made to realise that they have at
least four choices; to do it in your fashion, to count forwards in whole
years, months, ... from the beginning (or maybe backwards if it's a
time-to-wait), to use Thirty-Day Months, or not to call for such a
display after all.

Your code has the result that, if the months part is 0..11 respectively,
the upper limit on the days part is something like 30, 27/8, 30, 29,
..., 30 respectively, which is rather peculiar.

The essential for the employed coder is to get a solid specification
agreed with management.

Should those who foresee being required to serve a six-month sentence in
prison endeavour to have the interval include Feb 28 followed by Mar 1 ?

For some purposes, nominal 30-day months should be used :
<URL:http://www.merlyn.demon.co.uk/js-date3.htm#30DM>
<URL:http://www.merlyn.demon.co.uk/moredate.htm#30DM>
and there the US does not follow the same rule as the EU, but a more
complex one.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.