I have the following code-snippet:
if ([EndMonth].length == 1) {
EndMonth = '0' + EndMonth;
}
I get a month in return from a JS, and if it returns f.ex. 4,
I want to display 4 as 04. I thought the above would fix
this, but I'm not good at JS, and the above doesn't seem to
work.
My guess is that x.length is a string operator, and not an
integer-operator... Suggestions?
Thanks!
> Suggestions?
if ( EndMonth < 10) {
SEndMonth = '0' + EndMonth;
} else {
SEndMonth = ''+ EndMonth;
}
EndMonth = (EndMonth<10?'0':'') + EndMonth;
--
Thor Larholm
<URL: http://www.jibbering.com/faq/> FAQ for comp.lang.javascript
I don't really mind too much helping with things that look like homework
assignments but you've got to learn something first. Your code actually
creates a temporary array, then you compare the array length (1) to 1,
so your condition is always true. The expression:
[EndMonth]
is the short way of writing:
var tmpArray = new Array();
tmpArray[0] = EndMonth;
Now, back to your homework...
if (EndMonth < 10)
EndMonth = "0" + EndMonth;
Regards,
Steve
--
Martin Honnen
http://javascript.faqts.com/
http://home.t-online.de/home/martin.honnen/jsgoddies.html
>I get a month in return from a JS, and if it returns f.ex. 4,
>I want to display 4 as 04. I thought the above would fix
>this, but I'm not good at JS, and the above doesn't seem to
>work.
That is normally wanted in order that date or time can be displayed
properly; it is normally wanted more than once in a page. IMHO, it's
best to use a function :
function LZ(x) {return(x<0||x>9?"":"0")+x}
document.write(getYear()+'-'+LZ(getMonth()+1)+'-'+LZ(getDate())
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MSIE 4 ©
<URL: http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
<URL: http://www.merlyn.demon.co.uk/js-index.htm> Javascript maths, sources.
<URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.