With YYYYMMDD Trying to change MM to August

A

Alan Gresley

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 );
}
}
}
</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
 
E

Evertjan.

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.innerHTML;
if (!isNaN(mo)) {
mm.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.name=='MM')
mm.innerHTML = month[+mm.innerHTML];
};
</script>
 
R

RobG

Alan said:
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[

{
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 );


This is XHTML, right?

}
}
}
</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.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];
}
}
}
}
}
}
 
A

Alan Gresley

RobG said:
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.


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.



What does this do?

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


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.

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.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];
}
}
}
}
}

}



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").getElementsByTagName("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.innerHTML;
mm.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
 
A

Alan Gresley

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.
 
D

Dr J R Stockton

In comp.lang.javascript message <ff7bb677-559d-4d22-a9b4-c39c3de1cb2a@s1
3g2000prd.googlegroups.com>, Sun, 17 Feb 2008 21:28:51, Alan Gresley
<[email protected]> 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.
 
T

Thomas 'PointedEars' Lahn

RobG said:
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.
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", ...);
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 );


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);
}

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
 
T

Thomas 'PointedEars' Lahn

Alan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top