Simple Date Function Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Thomas

    Simple Date Function Error

    Hi, I've written a simple script to test the current date and perform
    an action depending on the result.
    The problem is, the date displays correctly as a complete date in an
    alert box but when I try to build the date from the components (i.e
    d.getMonth() etc...) the result is an incorrect date.
    How can the date be both correct and incorrect at the same time? The
    intial alert(d); displays correctly, but the alert(t + "/" + m + "/" +
    y); does not.
    Is it heisenbergs uncertainty principle at work?

    here's my code...

    <script language="javas cript" type="text/javascript">

    var d;
    d = new Date();

    //get the date properties
    t = d.getDay();
    m = d.getMonth();
    y= d.getYear();

    //display the full date from the Date object.
    alert(d);
    //display the date for debugging purposes
    alert(t + "/" + m + "/" + y);

    //test if it's between April and September
    if ((m > 4) && (m < 9))
    {
    //perform a conditional action
    alert("out of the season");
    }
    else
    {
    alert("in the season");
    }

    </script>

    it's a mystery... any ideas.

    appreciated.

    David Thomas.
  • Richard Cornford

    #2
    Re: Simple Date Function Error

    David Thomas wrote:
    <snip>[color=blue]
    > Is it heisenbergs uncertainty principle at work?[/color]

    The only uncertainty is yours in not knowing how javascript Date objects
    represent and report dates.

    <snip>[color=blue]
    > m = d.getMonth();[/color]
    <snip>[color=blue]
    > //test if it's between April and September
    > if ((m > 4) && (m < 9))[/color]
    <snip>

    The number returned from - getMonth - in zero based (zero is January).
    [color=blue]
    > it's a mystery... any ideas.[/color]

    RTFM.

    Richard.


    Comment

    • David Thomas

      #3
      Re: Simple Date Function Error

      Thanks for that. You are absolutely right. I actually did a bit of
      research and discovered the javascript 0 based getMonth(); scenario (i.e
      january = 0, february 1 etc...)
      I was also using getDay(); to return the date of the month when I should
      have been using getDate(); so that explains it. thanks for your
      feedback, I'm sure Mr Heisenberg would approve. The uncertainty was only
      within my limited sphere of knowledge at the time. When it comes to
      computers, there seems to be a rational explanation for everything.

      kind regards,

      D.Thomas.



      *** Sent via Devdex http://www.devdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Dr John Stockton

        #4
        Re: Simple Date Function Error

        JRS: In article <40e40c76$1$164 52$c397aba@news .newsgroups.ws> , seen in
        news:comp.lang. javascript, David Thomas <davidwhthomas@ hotmail.com>
        posted at Thu, 1 Jul 2004 13:07:03 :[color=blue]
        >Thanks for that. You are absolutely right. I actually did a bit of
        >research and discovered the javascript 0 based getMonth(); scenario (i.e
        >january = 0, february 1 etc...)
        >I was also using getDay(); to return the date of the month when I should
        >have been using getDate(); so that explains it. thanks for your
        >feedback, I'm sure Mr Heisenberg would approve.[/color]

        Professor Werner Heisenberg (1901-76) should be given his proper style.
        He had a Chair at Leipzig by 1930, and a Nobel Prize in 1932. I recall
        attending a lecture by him.

        If you had used the newsgroup FAQ with care, your problems should not
        have occurred; see below.

        Code should be indented, to show its logical structure.

        Dates should be presented with leading zeroes for month % day,
        preferably as YYYY-MM-DD or YYYY/MM/DD.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...