Add six months to a start date

S

sandy

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

Thank you
Sandy
 
M

Mike

Here is a simple script that may get you started.

var date = new Date();
var date6MonthsFromNow = new Date(date.getTime() + (182*24*60*60*1000));

the formula is simply [current time in mils + (#days in 6 months * #hrs in
day * #mins in hrs * #sec in min * #mils in sec)]

Regards
Mike
 
R

Randy Webb

Mike said:
Here is a simple script that may get you started.

var date = new Date();
var date6MonthsFromNow = new Date(date.getTime() + (182*24*60*60*1000));

the formula is simply [current time in mils + (#days in 6 months * #hrs in
day * #mins in hrs * #sec in min * #mils in sec)]

How many days are in 6 months depends directly on what 6 month span you
cover.

Of the 12 6 month spans that can be covered, only 2 have 182 days:

Jan-Jun 181 days
Feb-July 181 days
Mar-Aug 184 days
Apr-Sep 183 days
May-Oct 184 days
Jun-Nov 183 days
July-Dec 184 days
Aug-Jan 184 days
Sep-Feb 181 days
Oct-Mar 182 days
Nov-Apr 181 days
Dec-May 182 days

Means that your script is only right 1/6 (or ~17% of the time), not what
I would consider very efficient.

And that does not take into account leap day every 4 years, which is a
problem of its own and introduces 6 more possibilities:

Feb-July 182 days
Mar-Aug 185 days
Apr-Sep 184 days
May-Oct 185 days
Jun-Nov 184 days
July-Dec 185 days
Aug-Jan 185 days

but your correctness ratio is still 1/6.
 
R

rf

Dennis M. Marks said:
I have read the following message from (e-mail address removed) (sandy)
and have decided to lend my vast knowledge.

BTW: Six months after 01/01/04 is 07/01/04 not 06/01/04.

Over here six months after 01/01/04 is 01/07/04 :)

Cheers
Richard.
 
W

Willie Lau

Try this:

Date.prototype.addMonths = function(n)
{
this.setMonth(this.getMonth()+n);
return this;
}

var d = new Date().addMonths(6);

Regards,
Willie
 
W

Willie Lau

Try this:

Date.prototype.addMonths = function(n)
{
this.setMonth(this.getMonth()+n);
return this;
}

var d = new Date().addMonths(6);



Regards,
Willie
 
H

Harag

not tested... but I dont think that will work

say the date is 31st March 2004

if you add 6 months to that you would end up with 31st September
2004... Which is a problem because there isn't 31 days in September.

The best way is to always calculate in days...

6 months = 26 weeks (52/2)

26 weeks *7 = 182

So your best always adding 182 to the date.

HTH

Al.
 
H

Harag

I'd have to disagree with this and agree with mikes

they are 52 weeks in a year. or 12 months.

so 6 months is 26 weeks and a quarter is 13 weeks

they are 7 days a week

so 6 months = 26 * 7 = 182 days
or a quarter = 13 * 7 = 91 days

If you want to add just a month then that is the difficult one to do
as there isn't any set number of week so to work that out we go.

13 weeks / 3 = 4.3333333 weeks per month.
4.3333333 * 7 = 30.3333333 DAYS per month

so if you want to add one month you need to add the following number
of milliseconds to the date:

1000 * 60 * 60 * 24 * 30.33333333 = 2620800000

var date1MonthsFromNow = new Date(date.getTime() +
parseInt(30.3333333*24*60*60*1000));


so by adding 182 days, like mike has done here is the correct way to
add "6 months"



Al.


Mike said:
Here is a simple script that may get you started.

var date = new Date();
var date6MonthsFromNow = new Date(date.getTime() + (182*24*60*60*1000));

the formula is simply [current time in mils + (#days in 6 months * #hrs in
day * #mins in hrs * #sec in min * #mils in sec)]

How many days are in 6 months depends directly on what 6 month span you
cover.

Of the 12 6 month spans that can be covered, only 2 have 182 days:

Jan-Jun 181 days
Feb-July 181 days
Mar-Aug 184 days
Apr-Sep 183 days
May-Oct 184 days
Jun-Nov 183 days
July-Dec 184 days
Aug-Jan 184 days
Sep-Feb 181 days
Oct-Mar 182 days
Nov-Apr 181 days
Dec-May 182 days

Means that your script is only right 1/6 (or ~17% of the time), not what
I would consider very efficient.

And that does not take into account leap day every 4 years, which is a
problem of its own and introduces 6 more possibilities:

Feb-July 182 days
Mar-Aug 185 days
Apr-Sep 184 days
May-Oct 185 days
Jun-Nov 184 days
July-Dec 185 days
Aug-Jan 185 days

but your correctness ratio is still 1/6.
 
R

Randy Webb

Harag said:
I'd have to disagree with this and agree with mikes

they are 52 weeks in a year. or 12 months.

so 6 months is 26 weeks and a quarter is 13 weeks

That depends directly on your definition of a month.

they are 7 days a week

so 6 months = 26 * 7 = 182 days
or a quarter = 13 * 7 = 91 days

If you want to add just a month then that is the difficult one to do
as there isn't any set number of week so to work that out we go.

13 weeks / 3 = 4.3333333 weeks per month.
4.3333333 * 7 = 30.3333333 DAYS per month

so if you want to add one month you need to add the following number
of milliseconds to the date:

1000 * 60 * 60 * 24 * 30.33333333 = 2620800000

var date1MonthsFromNow = new Date(date.getTime() +
parseInt(30.3333333*24*60*60*1000));

And you will still be wrong. There are not 30.3333333 days in a month,
any month. So now you are wrong 6 out of 6 instead of 5 out of 6 times.
so by adding 182 days, like mike has done here is the correct way to
add "6 months"

No its not.

Please read the FAQ.
 
R

Richard Cornford

Harag said:
not tested... but I dont think that will work

say the date is 31st March 2004

if you add 6 months to that you would end up with 31st September
2004... Which is a problem because there isn't 31 days in September.

You should test, JavaScript Date objects are smarter than that. And:-

Wed Mar 31 00:00:00 UTC+0100 2004

- becomes -

Fri Oct 1 00:00:00 UTC+0100 2004

However:-

Sun Feb 29 00:00:00 UTC 2004

- becomes -

Sun Aug 29 00:00:00 UTC+0100 2004

-while -

Mon Mar 1 00:00:00 UTC 2004

- becomes -

Wed Sep 1 00:00:00 UTC+0100 2004

Six months after the last day of February is 3 days before the day 6
months after the first day in March (and presumably Aug 30th and 31st
aren't 6 months after any date).

So its over to Sandy to tell us what is going to be considered 6 months.

Richard.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Here is a simple script that may get you started.

var date = new Date();
var date6MonthsFromNow = new Date(date.getTime() + (182*24*60*60*1000));

the formula is simply [current time in mils + (#days in 6 months * #hrs in
day * #mins in hrs * #sec in min * #mils in sec)]

Balderdash. Evidently you have not been reading the group for long,
have not studied the FAQ, and have not thought much about either time or
date.

You have forgotten Summer Time (which, for a six month interval, usually
matters).

You have not allowed adequately for varying month lengths.

You have not considered the OP's ambiguity.
 
D

Dr John Stockton

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?.
 
W

Willie Lau

That's a valid point but the Date object does natively compensate for
the days, including leap years.

If there is no 31st September 2004, the next logical date 1st October
2004 is returned.

Regards,
Willie
 
F

Fabian

sandy hu kiteb:
Hi,
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.

As others have shown, "6 months" is ubject to many interpretations. If
this is for a business site, you might want to clarify it is being 26
weeks or whatever. Basically, while "6 month" is ok for casual use, you
should tell your customers the time period in an unambiguous way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top