Connecting Tech Pros Worldwide Forums | Help | Site Map

Difference in months

bryanilton@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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!


RobG
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Difference in months


bryanilton@gmail.com wrote:[color=blue]
> 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.
>[/color]

A good start is to have a read here:

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

--
RobG
Grant Wagner
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Difference in months


<bryanilton@gmail.com> wrote in message
news:1117632731.843371.262830@g14g2000cwa.googlegr oups.com...[color=blue]
>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.[/color]

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 <gwagner@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


bryanilton@gmail.com
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Difference in months


I'll give it a shot. Thanks for your help!

Mick White
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Difference in months


bryanilton@gmail.com wrote:
[color=blue]
> 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.[/color]


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
Dr John Stockton
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Difference in months


JRS: In article <FEkne.361$3d3.1814@news2.mts.net>, dated Wed, 1 Jun
2005 15:39:17, seen in news:comp.lang.javascript, Grant Wagner
<gwagner@agricoreunited.com> posted :[color=blue]
>
>
>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):[/color]
[color=blue]
> (anotherDay.getFullYear() - today.getFullYear()) * 12 +
> (anotherDay.getMonth() - today.getMonth())[/color]


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.

[color=blue]
>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.[/color]

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 <gLlne.499$g5.466@twister.nyroc.rr.com>, dated Wed, 1 Jun
2005 16:54:36, seen in news:comp.lang.javascript, Mick White <mwhite13BO
GUS@rochester.rr.com> posted :[color=blue]
>
>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.[/color]

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.
please-answer-here
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Difference in months


Mick White wrote:[color=blue]
> bryanilton@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.[/color]

6 different month lengths !!

Thats beyond my schoolknowledge or are we using a different calendar in
Denmark 8-)
[color=blue]
>
> Mick[/color]


Richard Cornford
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Difference in months


please-answer-here wrote:[color=blue]
> Mick White wrote:[/color]
<snip>[color=blue][color=green]
>> 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.[/color]
>
> 6 different month lengths !!
>
> Thats beyond my schoolknowledge or are we using a different
> calendar in Denmark 8-)[/color]

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

Richard.


Closed Thread