Difference in months

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bryanilton@gmail.com

    Difference in months

    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

    #2
    Re: Difference in months

    bryanilton@gmai l.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.demo n.co.uk/js-dates.htm>

    --
    RobG

    Comment

    • Grant Wagner

      #3
      Re: Difference in months

      <bryanilton@gma il.com> wrote in message
      news:1117632731 .843371.262830@ g14g2000cwa.goo glegroups.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.0322580645161 290322580645161 29032 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.get FullYear() - today.getFullYe ar()) * 12 +
      (anotherDay.get Month() - 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@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq


      Comment

      • bryanilton@gmail.com

        #4
        Re: Difference in months

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

        Comment

        • Mick White

          #5
          Re: Difference in months

          bryanilton@gmai l.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

          Comment

          • Dr John Stockton

            #6
            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@agrico reunited.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.get FullYear() - today.getFullYe ar()) * 12 +
            > (anotherDay.get Month() - 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.4 66@twister.nyro c.rr.com>, dated Wed, 1 Jun
            2005 16:54:36, seen in news:comp.lang. javascript, Mick White <mwhite13BO
            GUS@rochester.r r.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.demo n.co.uk/> - w. FAQish topics, links, acronyms
            PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
            Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

            Comment

            • please-answer-here

              #7
              Re: Difference in months

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


              Comment

              • Richard Cornford

                #8
                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.


                Comment

                Working...