Re: Date and time arithmetic

Tech-Archive recommends: Fix windows errors by optimizing your registry



JRS: In article <#dTePQ9QFHA.2132@xxxxxxxxxxxxxxxxxxxx>, dated Mon, 18
Apr 2005 15:04:06, seen in news:microsoft.public.scripting.jscript,
Trevor L. <tandcl@xxxxxxxxxxxxxxx> posted :
>I found this in reply to an earlier query
>
>You may find something in this FAQ:
>http://www.merlyn.demon.co.uk/js-index.htm

(a) It's not a FAQ, and it says so; don't cite what you've not read.
(b) He said he'd read it.

>--
>Microsoft MVP - ASP/ASP.NET

Please do not put signatures, especially with proper SigSeps, in the
middle of articles; it is annoying to those with newsreaders designed in
accordance with Usenet standards. Signatures should not be quoted,
unless being discussed.

>I found the topic quite interesting
>--
>Cheers,

Ditto.


>
>"Sam Hobbs" <samuel@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
>news:eI4CIz7QFHA.3356@xxxxxxxxxxxxxxxxxxxxxxx
>> How to advance a date and time by 29 days, 12 hours and 44 minutes? That
>> period of time is a lunar month; see:
>> http://encarta.msn.com/encyclopedia_761573665/Moon.html

No; it is only an average lunar month. IIRC, the variation is by a
significant fraction of a day. For a better average, add 2.9 s. Don't
trust the popular encyclopaedias to get things exactly right.

The Earth's orbit around the Sun is elliptical; the Moon's orbit about
the Earth is elliptical modified by the solar gravity gradient; and the
effects of other system bodies mat be enough to alter the time of New
Moon a bit.


>> The last new moon was April 8 at 20:34 UTC and the next new moon will be
>> May 8 at 8:48 UTC; see:
>> http://sunearth.gsfc.nasa.gov/eclipse/phase/phases.1999-2010.html

Fred Espenak knows a *lot* about such things; you should look at the
lengths of other lunar months. I expect you will find a variation about
the known mean.


>> I have tried a variety of ways to do the calculation. I have searched this
>> newsgroup and I have looked at J R Stockton's pages; see:
>> http://www.merlyn.demon.co.uk/js-dates.htm
>>
>> The code below is my last attempt. This works, except the time is about a
>> half hour too far. I assume the result should be 08:48:00 UTC but I get
>> 09:18:00 UTC.
>>
>> var Start
>> Start = new Date(Date.UTC(2005, 3, 8, 20, 34));
>> WScript.echo(Start.toUTCString());
>> with (Start) {
>> setDate(getDate()+29);
>> setHours(getHours()+12);
>> setMinutes(getMinutes()+44);
>> }
>> WScript.echo(Start.toUTCString());
>>
>>
>>
>> My results are:
>>
>> Fri, 8 Apr 2005 20:34:00 UTC
>> Sun, 8 May 2005 09:18:00 UTC

You have set a UTC date/time, and you have displayed a UTC date/time,
but you have done the intermediate work in Local Time. That would not
be good if the interval crossed a Summer Time/Winter Time transition.

Please give your localisation when discussing date/time; only a very
small group is allowed a half-hour Summer Time error.




WScript = {}
WScript.echo = document.writeln

var Start
Start = new Date(Date.UTC(2005, 3, 8, 20, 34));
WScript.echo(Start.toUTCString());
with (Start) {
setDate(getDate()+29);
setHours(getHours()+12);
setMinutes(getMinutes()+44);
}
WScript.echo(Start.toUTCString());

executed with RmEv in my js-quick.htm gives

Fri, 8 Apr 2005 20:34:00 UTC Sun, 8 May 2005 09:18:00 UTC

in agreement with your code; you could easily have done the calculation
by hand.



This gives the same result, using UTC arithmetic.

Start = new Date(Date.UTC(2005, (4-1), 8 + 29, 20 + 12, 34 + 44));
WScript.echo(Start.toUTCString());


--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
.