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

lengths?

P: n/a
Hey, I'm completely new to javascript, but I've had some experience
with Python (I know they're completely different; I don't want anyone
replying just to say so). I'm having a problem with getting the current
date and manipulating it as I need.

What I'm doing is a directory with a bunch of HTML files of the name
"DaysMM_DD.html", where MM and DD are 2 digit months and days
respectively. I want to get today's file, and am having trouble with
the code, seen below (with a couple stat markers):

==========================
<SCRIPT language=javascript>
<!--
var stats=""
var date=new Date();
var day=date.getDay();
var month=date.getMonth();
if (month.length==1){var full_month="0"+month; var
stats=stats+"<BR>month was too short\n";}else{var full_month=month; var
stats=stats+"<BR>month was the right length\n";};
if (day.length==1){var full_day="0"+day; var stats=stats+"<BR>day was
too short\n";}else{var full_day=day; var stats=stats+"<BR>day was the
right length\n";};
var URL="Days"+full_month+"_"+full_day+".html";
document.write("<H1 align=center><A HREF=\""+URL+"\">This
Way!</A></H1>");
document.write(stats)
-->
</script>
==========================

1. I'm getting the wrong date. It might be my browser has the wrong
date, but I doubt that.
2. The length comparisons seem to be going wrong. I tried before to do
month.string.length, but that seemed to be the wrong way.

What am I doing wrong???

Feb 5 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Du**********@gmail.com wrote:
Hey, I'm completely new to javascript, but I've had some experience
with Python (I know they're completely different; I don't want anyone
replying just to say so). I'm having a problem with getting the current
date and manipulating it as I need.

What I'm doing is a directory with a bunch of HTML files of the name
"DaysMM_DD.html", where MM and DD are 2 digit months and days
respectively. I want to get today's file, and am having trouble with
the code, seen below (with a couple stat markers):

==========================
<SCRIPT language=javascript>
<!--
var stats=""
var date=new Date();
var day=date.getDay();
var month=date.getMonth();
if (month.length==1){var full_month="0"+month; var
stats=stats+"<BR>month was too short\n";}else{var full_month=month; var
stats=stats+"<BR>month was the right length\n";};
if (day.length==1){var full_day="0"+day; var stats=stats+"<BR>day was
too short\n";}else{var full_day=day; var stats=stats+"<BR>day was the
right length\n";};
var URL="Days"+full_month+"_"+full_day+".html";
document.write("<H1 align=center><A HREF=\""+URL+"\">This
Way!</A></H1>");
document.write(stats)
-->
</script>
==========================

1. I'm getting the wrong date. It might be my browser has the wrong
date, but I doubt that.
2. The length comparisons seem to be going wrong. I tried before to do
month.string.length, but that seemed to be the wrong way.

What am I doing wrong???

confusion between day (monday, tuesday...) and date (01, 02...31)

Try this:

<SCRIPT language=javascript>
<!--
var today=new Date();
var theday=today.getDate();
if (theday<10) {zero="0";}
else {zero="";}
var themonth=today.getMonth();
tabmonth = new
Array("01","02","03","04","05","06","07","08","09" ,"10","11","12");
var URL="Days"+zero+theday+"_"+tabmonth[themonth]+".html";
document.write("<H1 align=center><A HREF="+URL+">This Way is
better!</A></H1>");
-->
</script>

for today, it should return "Days05_02.html" where 05 is the date and 02
the month (february)

Ignace de Witte
http://www.reunionislandguns.com
Feb 5 '06 #2

P: n/a
Thanks. Seems to have worked.

Feb 5 '06 #3

P: n/a
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Sat, 4 Feb 2006 18:59:14 remote, seen in
news:comp.lang.javascript, Du**********@gmail.com posted :
Hey, I'm completely new to javascript,
If you intend to continue posting here, you need to find out how to post
(and write) readily-readable javascript, and how to format a News reply
when using Google (though it would be better to use a proper
newsreader). Read the newsgroup FAQ.

What I'm doing is a directory with a bunch of HTML files of the name
"DaysMM_DD.html", where MM and DD are 2 digit months and days
respectively. I want to get today's file, and am having trouble with
the code, seen below (with a couple stat markers):

==========================
<SCRIPT language=javascript> // deprecated form
<!--
var stats=""
var date=new Date();
var day=date.getDay(); // Day of Week, non-ISO form
var month=date.getMonth(); // 0..11
... 1. I'm getting the wrong date. It might be my browser has the wrong
date, but I doubt that.
Easily checked by document.write(date) though that may give the date
in FFF.
2. The length comparisons seem to be going wrong. I tried before to do
month.string.length, but that seemed to be the wrong way.
Using String(month).length will give what you want. Most people
add a leading zero after testing numerically against 9 or 10, but
testing length is quite sensible especially in the general case where
there is a risk that the input is outside the expected range.

If you need to add a leading zero often enough (>2-4 times IMHO), better
to write a function for it; I use

function LZ(x) { return (x>=10||x<0?"":"0") + x }

which always returns a String.
What am I doing wrong???


Guessing.
with (new Date())
filename= "Days" + LZ(getMonth()+1) + "_" + LZ(getDate()) + ".html"

should suffice.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of 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.
Feb 5 '06 #4

P: n/a
Thanks for explaining how evil this Javascript support group is. I'll
remember to avoid it at all costs.

Feb 7 '06 #5

P: n/a
Du**********@gmail.com said the following on 2/6/2006 7:10 PM:
Thanks for explaining how evil this Javascript support group is.
Getting good answers is evil? Wow, didn't know that. But in any event,
this is *not* a "support group". It is a "discussion group". If you get
an answer, great. That is *not* its purpose though.
I'll remember to avoid it at all costs.


Don't make such wild promises, they might bite you later.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #6

P: n/a

Randy Webb wrote:
Du**********@gmail.com said the following on 2/6/2006 7:10 PM:
Thanks for explaining how evil this Javascript support group is.
Getting good answers is evil?


I was referring to the person who was telling me not to use Google
Groups.
Wow, didn't know that. But in any event,
this is *not* a "support group". It is a "discussion group". If you get
an answer, great. That is *not* its purpose though.
So direct me to a support group. It seems from the faq that this is
intended to be a support group.
I'll remember to avoid it at all costs.


Don't make such wild promises, they might bite you later.


Not too difficult to keep, since I don't use javascript so much.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Feb 7 '06 #7

P: n/a
Lee wrote on 07 feb 2006 in comp.lang.javascript:
You're not a customer. You're not always right.


Lee, but you are!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 7 '06 #8

P: n/a
Lee said the following on 2/7/2006 10:08 AM:
Du**********@gmail.com said:
Thanks for explaining how evil this Javascript support group is. I'll
remember to avoid it at all costs.


You should avoid using Google to post to this (or any other)
group,


Not sure I agree with that. You should avoid Google Groups unless you
understand it's pitfalls and how to get around them.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #9

P: n/a
JRS: In article <fe********************@comcast.com>, dated Tue, 7 Feb
2006 12:31:05 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Lee said the following on 2/7/2006 10:08 AM:
Du**********@gmail.com said:
Thanks for explaining how evil this Javascript support group is. I'll
remember to avoid it at all costs.


You should avoid using Google to post to this (or any other)
group,


Not sure I agree with that. You should avoid Google Groups unless you
understand it's pitfalls and how to get around them.


The evidence is that anyone with enough intelligence to use Google
Groups correctly has enough intelligence to benefit from using a well-
written standards-compliant newsreader.

That means a newsreader that can, as supplied, get SigSeps and dates
right.

Granted, some people are prevented by circumstances from using a proper
newsreader, some or all of the time.

YGCIB.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Feb 8 '06 #10

P: n/a
Dr John Stockton said the following on 2/7/2006 5:19 PM:
JRS: In article <fe********************@comcast.com>, dated Tue, 7 Feb
2006 12:31:05 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Lee said the following on 2/7/2006 10:08 AM:
Du**********@gmail.com said:
Thanks for explaining how evil this Javascript support group is. I'll
remember to avoid it at all costs.
You should avoid using Google to post to this (or any other)
group, Not sure I agree with that. You should avoid Google Groups unless you
understand it's pitfalls and how to get around them.


The evidence is that anyone with enough intelligence to use Google
Groups correctly has enough intelligence to benefit from using a well-
written standards-compliant newsreader.


Not sure they would "benefit" from a standards-compliant newsreader. I
can use Google Groups very well and the benefits of switching back to a
newsreader are minimal. There are other benefits to Google Groups that
do not exist in my newsreader (whether it is standards-compliant or
not). A Cross-Newsgroup search is difficult to do with the depth of
results that GG offers.
That means a newsreader that can, as supplied, get SigSeps and dates
right.
Mine does both, even though you don't agree.
Granted, some people are prevented by circumstances from using a proper
newsreader, some or all of the time.
Absolutely.
YGCIB.


There is just as much to be said about using un-explained Acronyms as
there is to be said about Google Groups though.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 8 '06 #11

P: n/a
JRS: In article <go********************@comcast.com>, dated Wed, 8 Feb
2006 11:08:21 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
YGCIB.


There is just as much to be said about using un-explained Acronyms as
there is to be said about Google Groups though.


You've learned YSCIB; its easy enough to guess YSGIB, I'd think.
Neither *is* unexplained.

Now MGCIB !

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of 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.
Feb 9 '06 #12

P: n/a
Dr John Stockton said the following on 2/8/2006 6:25 PM:
JRS: In article <go********************@comcast.com>, dated Wed, 8 Feb
2006 11:08:21 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
YGCIB. There is just as much to be said about using un-explained Acronyms as
there is to be said about Google Groups though.


You've learned YSCIB;


I guess I have now. Considering that the only person I can find that
uses it is you. But to each his own.
its easy enough to guess YSGIB, I'd think.

Now it is, but neither is broken.
Neither *is* unexplained.

Now MGCIB !


If you say so.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 9 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.