HighDots Forums  

lengths?

Javascript JavaScript language (comp.lang.javascript)


Discuss lengths? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
DustanGroups@gmail.com
 
Posts: n/a

Default lengths? - 02-04-2006 , 09:59 PM






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???


Reply With Quote
  #2  
Old   
Ignace de Witte
 
Posts: n/a

Default Re: lengths? - 02-05-2006 , 08:00 AM






DustanGroups (AT) gmail (DOT) com wrote:

Quote:
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


Reply With Quote
  #3  
Old   
DustanGroups@gmail.com
 
Posts: n/a

Default Re: lengths? - 02-05-2006 , 08:20 AM



Thanks. Seems to have worked.


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

Default Re: lengths? - 02-05-2006 , 04:57 PM



JRS: In article <1139108354.211819.167090 (AT) z14g2000cwz (DOT) googlegroups.com>
, dated Sat, 4 Feb 2006 18:59:14 remote, seen in
news:comp.lang.javascript, DustanGroups (AT) gmail (DOT) com posted :
Quote:
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.


Quote:
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.

Quote:
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.

Quote:
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.


Reply With Quote
  #5  
Old   
DustanGroups@gmail.com
 
Posts: n/a

Default Re: lengths? - 02-06-2006 , 07:10 PM



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


Reply With Quote
  #6  
Old   
Randy Webb
 
Posts: n/a

Default Re: lengths? - 02-06-2006 , 08:44 PM



DustanGroups (AT) gmail (DOT) com said the following on 2/6/2006 7:10 PM:
Quote:
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.

Quote:
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/


Reply With Quote
  #7  
Old   
DustanGroups@gmail.com
 
Posts: n/a

Default Re: lengths? - 02-07-2006 , 07:17 AM




Randy Webb wrote:
Quote:
DustanGroups (AT) gmail (DOT) 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.

Quote:
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.

Quote:
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.

Quote:
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #8  
Old   
Evertjan.
 
Posts: n/a

Default Re: lengths? - 02-07-2006 , 10:28 AM



Lee wrote on 07 feb 2006 in comp.lang.javascript:
Quote:
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)


Reply With Quote
  #9  
Old   
Randy Webb
 
Posts: n/a

Default Re: lengths? - 02-07-2006 , 12:31 PM



Lee said the following on 2/7/2006 10:08 AM:
Quote:
DustanGroups (AT) gmail (DOT) 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/


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

Default Re: lengths? - 02-07-2006 , 05:19 PM



JRS: In article <feOdnXn53IJTSnXeRVn-uw (AT) comcast (DOT) com>, dated Tue, 7 Feb
2006 12:31:05 remote, seen in news:comp.lang.javascript, Randy Webb
<HikksNotAtHome (AT) aol (DOT) com> posted :
Quote:
Lee said the following on 2/7/2006 10:08 AM:
DustanGroups (AT) gmail (DOT) 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.


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.