HighDots Forums  

Simple Date Function Error

Javascript JavaScript language (comp.lang.javascript)


Discuss Simple Date Function Error in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
David Thomas
 
Posts: n/a

Default Simple Date Function Error - 07-01-2004 , 03:02 AM






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="javascript" 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.

Reply With Quote
  #2  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Simple Date Function Error - 07-01-2004 , 06:19 AM






David Thomas wrote:
<snip>
Quote:
Is it heisenbergs uncertainty principle at work?
The only uncertainty is yours in not knowing how javascript Date objects
represent and report dates.

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

The number returned from - getMonth - in zero based (zero is January).

Quote:
it's a mystery... any ideas.
RTFM.

Richard.




Reply With Quote
  #3  
Old   
David Thomas
 
Posts: n/a

Default Re: Simple Date Function Error - 07-01-2004 , 09:07 AM



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!

Reply With Quote
  #4  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Simple Date Function Error - 07-01-2004 , 03:19 PM



JRS: In article <40e40c76$1$16452$c397aba (AT) news (DOT) newsgroups.ws>, seen in
news:comp.lang.javascript, David Thomas <davidwhthomas (AT) hotmail (DOT) com>
posted at Thu, 1 Jul 2004 13:07:03 :
Quote:
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.
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.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.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.