JRS: In article <
[email protected]>, seen
Using Java script I am trying to create code where when you place in
the start date it automatically calculates 6 months for the
experations date. For example when I place 01/01/04 as the issue date
the experation date should automaically generate as 06/01/04. I would
appreciate it if anyone could help me.
Before asking in a newsgroup, one should seek its FAQ. The regularly-
posted FAQ of this group gives clear, if inconspicuous, advice on
date/time questions; it is only necessary to search it for the word
"Date".
In an international medium such as this, dates should be expressed
unambiguously and not in a local manner; the latter only gives people a
reason to poke fun. Moreover, it is generally wiser to choose, in
examples, 12 < DayOfMonth < 29. You mean 2004-01-13 -> 2004-07-13.
For international use, one must assume that Summer Time may occur, even
if you yourself live in Beijing or Honolulu. Therefore, any method
using seconds or milliseconds is suspect, and needs some form of
rounding; otherwise, the date will sometimes be wrong.
Willie Lau is about right; his code is efficient, but assumes that the
javascript default action in going to a date past the end of the month,
such as 31 Jun or 30 Feb, is suitable. That means that 6 months from
Aug 31 gives Mar 2 or Mar 3.
Most applications, under those circumstances, want to end up with the
last day of the sixth month ahead, or the first day of the seventh.
See <URL:
http://www.merlyn.demon.co.uk/js-date2.htm#incr> and
<URL:
http://www.merlyn.demon.co.uk/js-date1.htm#MC>.
The following function takes a Date Object, a number of months, and a
boolean to show whether such cases go to the last or first of a month.
The result is the altered object.
function AlterMonth(DObj, By, Back) { // Back is boolean
with (DObj) { var Xd = getDate() ; setMonth(getMonth() + By)
if (Xd != getDate()) setDate(Number(Back)) // 0 or 1, as needed
} }
N.B. I don't know whether setDate(0) works in NS4 for the Mac, but I
suspect not; and how about setDate(31) in a short month?.