By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Difference in months

P: n/a
I was hoping someone could help me with this.. I'm not very familiar
with javascript but I'm pretty sure it can help me with what I need to
accomplish. I've tried piecing several pieces of script together but
I've had no luck so far.

What I need to do is have a user input for Year, Month and day
(preferably drop down) and compare the input to today's date and
display the difference in months.

Any help with this would be appreciated.

Thanks!

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
br********@gmail.com wrote:
I was hoping someone could help me with this.. I'm not very familiar
with javascript but I'm pretty sure it can help me with what I need to
accomplish. I've tried piecing several pieces of script together but
I've had no luck so far.

What I need to do is have a user input for Year, Month and day
(preferably drop down) and compare the input to today's date and
display the difference in months.


A good start is to have a read here:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm>

--
RobG
Jul 23 '05 #2

P: n/a
<br********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I was hoping someone could help me with this.. I'm not very familiar
with javascript but I'm pretty sure it can help me with what I need to
accomplish. I've tried piecing several pieces of script together but
I've had no luck so far.

What I need to do is have a user input for Year, Month and day
(preferably drop down) and compare the input to today's date and
display the difference in months.


I think you'll need to be more specific in terms of your requirements.

Assuming today is June 1.

If I enter June 2, how many "months" different is that from today? 0
months different? 0.0333333... months (1 day divided by the 30 days in
this month) different?

How about if I enter July 31? Is that 1 month different, 2 months
different (since July 31 is more than twice the number of days in June
away from June 1) or 2.032258064516129032258064516129032 months
different (2 times 30 days in June + 1/31 days in July)?

I'm not trying to be difficult, when you say "how many months away is
this day from this other day" these are the types of real things you
need to think about.
Anyway, if you just want it as an integer where a day in the same month
is 0 months and any day in any other month is the number of months
different, then it's (something like):

<script type="text/javascript">
var today = new Date();
var anotherDay = new Date('May 6, 2006');
alert(Math.abs(
(anotherDay.getFullYear() - today.getFullYear()) * 12 +
(anotherDay.getMonth() - today.getMonth())
));
</script>

But you've still got some problems... the date the user enters may not
be in a format that new Date() understands, so you'll need to parse it
and ensure that it is.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3

P: n/a
I'll give it a shot. Thanks for your help!

Jul 23 '05 #4

P: n/a
br********@gmail.com wrote:
I was hoping someone could help me with this.. I'm not very familiar
with javascript but I'm pretty sure it can help me with what I need to
accomplish. I've tried piecing several pieces of script together but
I've had no luck so far.

What I need to do is have a user input for Year, Month and day
(preferably drop down) and compare the input to today's date and
display the difference in months.

Any help with this would be appreciated.

Consider that a month is an imprecise measurement, here in NY there are
at least 6 different month "lengths". And this disregards year end
tweakings of the "atomic" clock.

Mick
Jul 23 '05 #5

P: n/a
JRS: In article <FE****************@news2.mts.net>, dated Wed, 1 Jun
2005 15:39:17, seen in news:comp.lang.javascript, Grant Wagner
<gw*****@agricoreunited.com> posted :


Anyway, if you just want it as an integer where a day in the same month
is 0 months and any day in any other month is the number of months
different, then it's (something like): (anotherDay.getFullYear() - today.getFullYear()) * 12 +
(anotherDay.getMonth() - today.getMonth())

One can also calculate an "Absolute Month Number" like
function AWM(Y, M) { return Y*12+M }
and then subtract. It's numerically equivalent, and less efficient; but
the concept can be helpful in more complicated cases.

But you've still got some problems... the date the user enters may not
be in a format that new Date() understands, so you'll need to parse it
and ensure that it is.
Necessary but not sufficient. It is also necessary that new Date(), for
all users, interprets it in the same way as the user expects. And that
must be tested for the Nth of month M, where N != M and N < 13.


In article <gL**************@twister.nyroc.rr.com>, dated Wed, 1 Jun
2005 16:54:36, seen in news:comp.lang.javascript, Mick White <mwhite13BO
GU*@rochester.rr.com> posted :
Consider that a month is an imprecise measurement, here in NY there are
at least 6 different month "lengths". And this disregards year end
tweakings of the "atomic" clock.


And Lunar, Hebrew, Islamic, etc., months.

Year middle tweakings are equally favoured, and have been approximately
equally common. Tweakings at the end of odd quarters are also allowed,
but less favoured, and have not so far been needed.

There will be none at the end of this month; but the length of time
since the previous leap second now is and until the next one will remain
longer than it has ever been before (IIRC).

--
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 23 '05 #6

P: n/a
Mick White wrote:
br********@gmail.com wrote:

Consider that a month is an imprecise measurement, here in NY there
are at least 6 different month "lengths". And this disregards year end
tweakings of the "atomic" clock.
6 different month lengths !!

Thats beyond my schoolknowledge or are we using a different calendar in
Denmark 8-)

Mick

Jul 23 '05 #7

P: n/a
please-answer-here wrote:
Mick White wrote:

<snip>
Consider that a month is an imprecise measurement, here
in NY there are at least 6 different month "lengths". And
this disregards year end tweakings of the "atomic" clock.


6 different month lengths !!

Thats beyond my schoolknowledge or are we using a different
calendar in Denmark 8-)


That sounds about right. 4 Obvious differences in the length of months
in days and two months containing daylight savings/summer time
adjustments.

Richard.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.