473,490 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 41579
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15912
by: David Kuhn | last post by:
I have a query with a date field criteria of: Between And When the query is run, I am asked for the Start date and then the End Date. So far, so good. The records returned are all those in...
7
2945
by: CheezIt2831 | last post by:
hello, I have 5 tables(Alpha, Bravo, Charlie, Service, Trans). What i am trying to do is be able to select a start date and ending date for a report and include fields from the 5 tables. this is my...
1
1734
by: srikanthch3 | last post by:
Hi I had problem in comparision of the start date and end date vba in excell.if u give the end date 2/2/07 and start date 3/2/07, it is accepting.so any one can help. Srikanth
0
1483
by: Scott | last post by:
I have a report that summarizes attendance occurances for my team of 50 employees. Right now the date values are entered by the user before report execution. I want the start date to be...
1
2789
by: Del | last post by:
I have a parameter query that requires the user to enter a Start Date: and End Date: and pull data between that date range. I am currently using the following parameter; Select * From mytable...
0
1801
by: =?Utf-8?B?QW5pdGhh?= | last post by:
Hi, Currently for Announcement library a custom column "publish start date" is added and under the search result webpart changed the xslt by filtering the result based on the publish start date...
13
4932
by: Tim Mullin | last post by:
Hello all, I'm hoping to find an answer to a question I have using forms in Access 2007. I have a form (source: table) that has three fields: Start Date Term (number of months) End Date
3
2714
by: Jared Elyea | last post by:
I had no experience in VB before writing this so bare with me... the function below seems to work for certain cases, but not all (in some cases it is a week behind). Can anybody help me figure this...
1
2168
by: divya birla | last post by:
Posted - 03/29/2011 : 06:34:20 I have a table which contain Topic, Sequence number and no of turns. User input the Single Start date and on the basis of which series of start date and end...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.