469,287 Members | 2,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

Add six months to a start date

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
Jul 20 '05 #1
16 40017
I have read the following message from sa**********@yahoo.com (sandy)
and have decided to lend my vast knowledge.

The writer said:
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


and my reply is:
The following must be considered before an answer can be given. What if
the date is the 31st and there is no 31st six months later?

What is six months after 03/31/04?

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

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #2
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
Jul 20 '05 #3
Mike <mi*****************@synovic.com> wrote:
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.

--
Randy

Jul 20 '05 #4
rf

"Dennis M. Marks" <de******@domain.invalid> wrote in message
news:120120041543585567%de******@domain.invalid...
I have read the following message from sa**********@yahoo.com (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.
Jul 20 '05 #5
Try this:

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

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

Regards,
Willie
Jul 20 '05 #6
Try this:

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

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

Regards,
Willie
Jul 20 '05 #7

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.

On 12 Jan 2004 23:22:38 -0800, wi****@brainjacked.com (Willie Lau)
wrote:
Try this:

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

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

Regards,
Willie


Jul 20 '05 #8

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));

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

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

Al.
On Mon, 12 Jan 2004 21:33:07 -0500, Randy Webb
<hi************@aol.com> wrote:
Mike <mi*****************@synovic.com> wrote:
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.


Jul 20 '05 #9
Harag wrote:
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.

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

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.

--
Randy

Jul 20 '05 #10
"Harag" <ha***@REMOVETHESECAPITALSsofthome.net> wrote in message
news:e9********************************@4ax.com...

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.
Jul 20 '05 #11
I have read the following message from sa**********@yahoo.com (sandy)
and have decided to lend my vast knowledge.

The writer said:
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


and my reply is:
It all depends on the use this is being put to.

Is it a subscription for 6 monthly issues, 26 weekly issues, 182 daily
issues. In this case add days, weeks or months to the date.

Is it a reminder that must appear one day prior to a date. For example
the last day of the month six months from now. Setting the start date
to the 31st may cause a problem.

There is no reason to argue about the definition of 6 months without
knowing the purpose of adding 6 months.

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #12
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

Harag <ha***@REMOVETHESECAPITALSsofthome.net> wrote in message news:<e9********************************@4ax.com>. ..
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.

On 12 Jan 2004 23:22:38 -0800, wi****@brainjacked.com (Willie Lau)
wrote:
Try this:

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

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

Regards,
Willie

Jul 20 '05 #13
JRS: In article <-7********************@comcast.com>, seen in
news:comp.lang.javascript, Mike <mi*****************@synovic.com> posted
at Mon, 12 Jan 2004 19:46:16 :-
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #14
JRS: In article <fb**************************@posting.google.com >, seen
in news:comp.lang.javascript, sandy <sa**********@yahoo.com> posted at
Mon, 12 Jan 2004 15:22:41 :-
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?.

--
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.
Jul 20 '05 #15
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.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #16
Fabian wrote:
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.


If you actually want "6 months" (that is, the same day of the month 6 months
from now, or the last day of the month if that isn't possible), then you
could use:

Date.prototype.addMonths = function (n) {
var day = this.getDate();
this.setMonth(this.getMonth() + n);
if (this.getDate() < day) {
this.setDate(1);
this.setDate(this.getDate() - 1);

}
return this;
}
var d = new Date("March 31, 2003");
alert(d.addMonths(6));

I haven't tested it with every possible rational date, but I believe it will
always set the date to the same day of the month it is now except in the
cases where the current day of the month does not exist in the new month
(such as "6 months" added to March 31).

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by =?Utf-8?B?QW5pdGhh?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.