Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > With YYYYMMDD Trying to change MM to August

Reply
Thread Tools

With YYYYMMDD Trying to change MM to August

 
 
Alan Gresley
Guest
Posts: n/a
 
      02-18-2008
Hello

I am new to javascript. What I would like is to change the value of a
month in MM format into the months proper name. This is the code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Data Table</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml;
charset=utf-8" />

<script type="text/javascript">

function getElements()
{
var x=document.getElementsByName("span");
{
var month=x = new Array();
month [01] = "January";
month [02] = "February";
month [03] = "March";
month [04] = "April";
month [05] = "May";
month [06] = "June";
month [07] = "July";
month [08] = "August";
month [09] = "September";
month [10] = "October";
month [11] = "November";
month [12] = "December";

for (i=01;i<x.length;i++)
{
document.write(x[i] );
}
}
}
</script>

</head>
<body>

<table>
<thead>
<tr><th>Year</th><th>?</th><th>Forname(s)</th><th>Surname</th></tr>
</thead>

<tbody>
<tr><td>1840<span>02</span>03</td><td></td><td>Elizabeth</
td><td><a>ABBOTT</a></td></tr>
<tr><td>1800<span>03</span>03</td><td></td><td>Esther</td><td>ABBOTT</
td></tr>
<tr><td>1830<span>04</span>03</td><td></td><td>Franciscus</
td><td>ALEXANDER</td></tr>
<tr><td>1800<span>01</span>20</td><td></td><td>?</td><td>ALLEN</td></
tr>
</tbody>
</table>

</body>
</html>


So the first cell in the first row would appear "1840February03"

Any assitance would gladly be welcome, Thank you

Alan
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      02-18-2008
McKirahan wrote on 18 feb 2008 in comp.lang.javascript:

> <script type="text/javascript">
> function Months() {
> var month = new Array();
> month[1] = "January";
> month[2] = "February";
> month[3] = "March";
> month[4] = "April";
> month[5] = "May";
> month[6] = "June";
> month[7] = "July";
> month[8] = "August";
> month[9] = "September";
> month[10] = "October";
> month[11] = "November";
> month[12] = "December";
> var mm = document.getElementsByName("MM");
> for (var i=0; i<mm.length; i++) {
> var mo = mm[i].innerHTML;
> if (!isNaN(mo)) {
> mm[i].innerHTML = month[mo*1];
> }
> }
>}
> </script>
>


For IE do this:

<script type='text/javascript'>
function Months() {
var month = ['','January','February','March',
'April','May','June','July','August',
'September','October','November','December'];
var mm = document.getElementsByTagName('span');
for (var i=0; i<mm.length; i++)
if (mm[i].name=='MM')
mm[i].innerHTML = month[+mm[i].innerHTML];
};
</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      02-18-2008


Alan Gresley wrote:

> Hello
>
> I am new to javascript. What I would like is to change the value of a
> month in MM format into the months proper name. This is the code.
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
>
> <head>
> <title>Data Table</title>
> <meta http-equiv="Content-Type" content="application/xhtml+xml;
> charset=utf-8" />


If you really are serving this as XHTML, then you clearly don't care
about users of IE or of browsers more than a few years old.


> <script type="text/javascript">


So it would be wise to use a correct type:

type="application/x-javascript"


>
> function getElements()


All script within the script element should be enclosed in #PCDATA
quotes:

<![CDATA[

<URL: http://www.w3.org/TR/xhtml1/#h-4.8 >

> {
> var x=document.getElementsByName("span");


That should be getElementsByTagName, unless you have actually given
some elements a name attribute of "span".

> {
> var month=x = new Array();


I don't see the point of declaring two variables with the same value
when only one is required.

> month [01] = "January";
> month [02] = "February";
> month [03] = "March";
> month [04] = "April";
> month [05] = "May";
> month [06] = "June";
> month [07] = "July";
> month [08] = "August";
> month [09] = "September";
> month [10] = "October";
> month [11] = "November";
> month [12] = "December";


You might find maintenance simpler if declared as:

var months = ['January','February','March',...'December'];


> for (i=01;i<x.length;i++)


Counters should not be allowed to escape into the global space, keep
them local:

for (var i=01; i<x.length; i++)


> {
> document.write(x[i] );


This is XHTML, right?

<URL: http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite >

> }
> }
> }
> </script>
>
> </head>
> <body>
>
> <table>
> <thead>
> <tr><th>Year</th><th>?</th><th>Forname(s)</th><th>Surname</th></tr>
> </thead>
>
> <tbody>
> <tr><td>1840<span>02</span>03</td><td></td><td>Elizabeth</
> td><td><a>ABBOTT</a></td></tr>


You should probably give your tbody an ID, then use it's rows
collection to grab the first cell of each row, then
getElementsByTagName so that you can at least use spans elsewhere
without creating errors. e.g.

<tbody id="someDates">
...
</tbody>


Script:

function doMonths(id) {
var el = (typeof id == 'string')? document.getElementById(id) : id;
var i, row, cell, span, monthText;
var months = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September',
'October', 'November', 'December'];

if (el && el.rows) {
rows = el.rows;
i = rows.length;

while (i--) {
cell = rows[i].cells[0];

if (cell) {
span = cell.getElementsByTagName('span')[0];

if (span) {
monthText = parseInt(span.innerHTML, 10);

if (typeof monthText == 'number') {
span.innerHTML = months[monthText - 1];
}
}
}
}
}
}


--
Rob
 
Reply With Quote
 
Alan Gresley
Guest
Posts: n/a
 
      02-18-2008
RobG wrote:

> If you really are serving this as XHTML, then you clearly don't care
> about users of IE or of browsers more than a few years old.
>
> > <script type="text/javascript">

>
> So it would be wise to use a correct type:
>
> type="application/x-javascript"



Mmmmm, My usual pursuit is exposing IE bugs with CSS support and I
always use a xml prolog on my pages. Ready for the big jump into
serving xhtml as true xml. Unfortunately I don't think IE8 will
support application/xhtml+xml so I can only hope my users will switch
to a better browser. Currently about 70% of users to my Genealogy
sites use IE



> All script within the script element should be enclosed in #PCDATA
> quotes:
>
> <![CDATA[



Is this just for validation purposes? I plan to link to the JS file
via the script element.

<script type="text/javascript" src="month-format.js"></script>


> <URL:http://www.w3.org/TR/xhtml1/#h-4.8>



What does this do?


> > var x=document.getElementsByName("span");

>
> That should be getElementsByTagName, unless you have actually given
> some elements a name attribute of "span".
>
> > {
> > var month=x = new Array();

>
> I don't see the point of declaring two variables with the same value
> when only one is required.



I was getting mix up with name and tagName (I'm a newbie with
javascript). I do see that x=document and month=x is doing nothing
special. I still have to learn when to you use x= or =x.


> > for (i=01;i<x.length;i++)

>
> Counters should not be allowed to escape into the global space, keep
> them local:
>
> for (var i=01; i<x.length; i++)
>
>
> This is XHTML, right?
>
> <URL:http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite>



Can you please clarify "escape into the global space?" I see that has
added a variable before the counter. Do you mean this is meant for
XTHML? If so the answer is yes.



> You should probably give your tbody an ID, then use it's rows
> collection to grab the first cell of each row, then
> getElementsByTagName so that you can at least use spans elsewhere
> without creating errors. e.g.
>
> <tbody id="someDates">
> ...
> </tbody>
>
> Script:
>
> function doMonths(id) {
> var el = (typeof id == 'string')? document.getElementById(id) : id;
> var i, row, cell, span, monthText;
> var months = ['January', 'February', 'March', 'April', 'May',
> 'June', 'July', 'August', 'September',
> 'October', 'November', 'December'];
>
> if (el && el.rows) {
> rows = el.rows;
> i = rows.length;
>
> while (i--) {
> cell = rows[i].cells[0];
>
> if (cell) {
> span = cell.getElementsByTagName('span')[0];
>
> if (span) {
> monthText = parseInt(span.innerHTML, 10);
>
> if (typeof monthText == 'number') {
> span.innerHTML = months[monthText - 1];
> }
> }
> }
> }
> }
>
> }
>
> --
> Rob



I have not tested your code yet but I change parts of what McKirahan
showed to create this working code.

<html>
<head>
<title>Data Table</title>

<script type="text/javascript">

window.onload=function Months() {
var mm =
document.getElementById("data").getElementsByTagNa me("span");
var month = new Array();
month[1] = "Jan";
month[2] = "Feb";
month[3] = "Mar";
month[4] = "Apr";
month[5] = "May";
month[6] = "Jun";
month[7] = "Jul";
month[8] = "Aug";
month[9] = "Sep";
month[10] = "Oct";
month[11] = "Nov";
month[12] = "Dec";
for (var i=0; i<mm.length; i++) {
var mo = mm[i].innerHTML;
mm[i].innerHTML = month[mo*1];
}
}

</script>

</head>
<body>

<table id="data">
<thead>
<tr><th>Year</th><th>?</th><th>Forname(s)</th><th>Surname</th></tr>
</thead>

<tbody>

<tr><td>1840<span>02</span>03</td><td></td><td>Elizabeth</
td><td><a>ABBOTT</a></td></tr>
<tr><td>1800<span>03</span>03</td><td></td><td>Esther</td><td>ABBOTT</
td></tr>
<tr><td>1830<span>04</span>03</td><td></td><td>Franciscus</
td><td>ALEXANDER</td></tr>
<tr><td>1800<span>01</span>20</td><td></td><td>?</td><td>ALLEN</td></
tr>

</tbody>

</table>

</body>
</html>


I knew I could not use the name attribute for the span element, but
having working code to tamper with I realized soon that I could just
drop the attribute all together. I remove the button and this loop:

if (!isNaN(mo)) {}

which I assume "if" is a user action like pressing the button (or if
some other value is true). I relaize that i had to add a onload
funtion.


window.onload=function Months()


A live example which also includes some JS for sorting columns.

http://css-class.com/javascript/datatable3.htm


I would really like to remove the embedded JS.

http://css-class.com/javascript/datatable4.htm

with this JS.

http://css-class.com/cssscript/month-format.js


And getting it to fire with this.


<script type="text/javascript">

addEvent(window, 'load', doIEStuff);

</script>


Does this all seem correct. I will have to test if I can incorperate
your array Rob.

var months = ['January', 'February', 'March' ...


Thank you, Alan
 
Reply With Quote
 
Alan Gresley
Guest
Posts: n/a
 
      02-18-2008
Randy Webb wrote:

[snip]
> I am curious. If 70% of your users use IE, why do you try to give it a
> resource that it has no clue what it is, and thereby put IE in quirks
> mode and making those bugs that much more prevalent?
>
> --
> Randy



I good question, I will answer this from a CSS and XHTML perspective.
IE5 is always in quirks mode. The xml prolog puts IE6 into quirks mode
and with IE7 the xml prolog causes IE7 to for fall *+html hack. * =
xml prolog. So which resource am I giving to what? Do you mean some
code that IE doesn't understand? I know that I write clean code and IE
just treats it as tag soup anyway. IE6 in quirks mode is actually
"lest buggy" with CSS and allows me to feed the same CSS to IE5 and
IE6. Does IE6 in quirks mode make javascript more buggy.

--
Alan
http://css-class.com/

 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      02-18-2008
In comp.lang.javascript message <ff7bb677-559d-4d22-a9b4-c39c3de1cb2a@s1
3g2000prd.googlegroups.com>, Sun, 17 Feb 2008 21:28:51, Alan Gresley
<(E-Mail Removed)> posted.

I thought I'd answer the question suggested by the Subject line :

var months = [,'January','February','March',,,,,,,,,'December'];

S = "yyyy02dd"

T = S.replace(/^(....)(..)(..)$/,
function($0, $1, $2, $3) { return $1 + months[+$2] + $3 } )

gives the value "yyyyFebruarydd" to T.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-22-2008
RobG wrote:
> Alan Gresley wrote:
>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
>> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
>>
>> <head>
>> <title>Data Table</title>
>> <meta http-equiv="Content-Type" content="application/xhtml+xml;
>> charset=utf-8" />

>
> If you really are serving this as XHTML, then you clearly don't care
> about users of IE or of browsers more than a few years old.


And if he is really serving this as advertised, the XML parser that is used
then will not care about the advertisement. At this point, the parse tree
has been built because the markup is well-formed, or there is no document.
However, content provided in an encoding that does not match the HTTP header
or XML declaration value, or that which is determined by reading the BOM,
can not be well-formed.

>> <script type="text/javascript">

>
> So it would be wise to use a correct type:
>
> type="application/x-javascript"


There is nothing correct about this. You were (not) looking for
"application/javascript", see http://PointedEars.de/scripts/test/mime-types
(updated).

>> function getElements()

>
> All script within the script element should be enclosed in #PCDATA
> quotes:
>
> <![CDATA[
>
> <URL: http://www.w3.org/TR/xhtml1/#h-4.8 >


You misunderstood the Specification. A CDATA section declaration
is only required if the source code contains markup characters.

This particular script requires the declaration because it has the
`<' character (an STAGO token in PCDATA) in it.

>> {
>> var month=x = new Array();

>
> I don't see the point of declaring two variables with the same value
> when only one is required.
>
>> month [01] = "January";
>> month [02] = "February";
>> month [03] = "March";
>> month [04] = "April";
>> month [05] = "May";
>> month [06] = "June";
>> month [07] = "July";
>> month [08] = "August";
>> month [09] = "September";
>> month [10] = "October";
>> month [11] = "November";
>> month [12] = "December";

>
> You might find maintenance simpler if declared as:
>
> var months = ['January','February','March',...'December'];


or

... = new Array("January", ...);

>> for (i=01;i<x.length;i++)

>
> Counters should not be allowed to escape into the global space, keep
> them local:


There is also an octal numeric literal that might not be supported by the
implementation: `01'. It should be 0 for "January". And reducing property
accesses increases efficiency, of course:

for (var i = 0, len = x.length; i < len; i++)

> for (var i=01; i<x.length; i++)
>
>
>> {
>> document.write(x[i] );

>
> This is XHTML, right?
>
> <URL: http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite >


The statement describes the current situation, however the rationale (that
XML would be the reason) is wrong. The statement and the current situation
both contradict the W3C DOM Level 2 HTML Specification (which applies for
XHTML 1.0 documents as well).

Also, consecutive calls of document.write() are inefficient. The output
string should be constructed first and written only once:

// or: ... = new Array();
var out = [];

for (var i = 0, len = x.length; i < len; i++)
{
out.push(x[i]);
}

document.write(out.join(""));

In this case,

document.write(out.join("<br/>\n"));

proves to be more useful. The OP should note that document.write() does not
write lines. There is document.writeln() for that, but it doesn't help with
the markup, and ISTM enjoys not that much a support in implementations.

> function doMonths(id) {
> var el = (typeof id == 'string')? document.getElementById(id) : id;


function doMonths(el)
{
if (typeof el == 'string')
{
// add feature tests here
el = document.getElementById(el);
}

// ...
}

> var i, row, cell, span, monthText;


It would appear that maintenance becomes a lot easier if one takes advantage
of the language feature that variables can be declared near the position
where they are first used instead of on the top of all the code.

> if (el && el.rows) {
> rows = el.rows;
> i = rows.length;
>
> while (i--) {


I recommend using `for' statements for their compactness instead, which
again makes maintenance easier.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-22-2008
Alan Gresley wrote:
> Randy Webb wrote:
> [snip]
>> I am curious. If 70% of your users use IE, why do you try to give it a
>> resource that it has no clue what it is, and thereby put IE in quirks
>> mode and making those bugs that much more prevalent?
>> [...]

>
> I good question, I will answer this from a CSS and XHTML perspective.
> IE5 is always in quirks mode. The xml prolog puts IE6 into quirks mode
> and with IE7 the xml prolog causes IE7 to for fall *+html hack. * =
> xml prolog. So which resource am I giving to what? Do you mean some
> code that IE doesn't understand? I know that I write clean code and IE
> just treats it as tag soup anyway. IE6 in quirks mode is actually
> "lest buggy" with CSS and allows me to feed the same CSS to IE5 and
> IE6. Does IE6 in quirks mode make javascript more buggy.


Your logic is flawed.

Here are the facts: IE 5 does not support XHTML. IE 6 does not support
XHTML. IE 7 does not support XHTML. The XML declaration is not required
for a valid X(HT)ML document.

And finally, rendering in "Quirks Mode" (in IE it is "Compatibility Mode"
instead) has nothing to do with the markup being properly parsed, nor have
any CSS quirks a layout engine might have. Serving markup to user agents
that are known not to support it properly is simply a recipe for disaster.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
find difference in days from YYYYMMDD to YYYYMMDD Konstantinos Pachopoulos Python 5 09-23-2007 01:09 AM
Re: [pyconuk] pyweek is happening august 02 - august 09 John Pinner Python 0 08-05-2007 08:37 AM
Re: [pygame] pyweek is happening august 02 - august 09 Laura Creighton Python 0 08-05-2007 12:40 AM
Re: [pygame] pyweek is happening august 02 - august 09 Luke Paireepinart Python 0 08-04-2007 07:25 PM
pyweek is happening august 02 - august 09 Laura Creighton Python 0 08-04-2007 06:32 PM



Advertisments