Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Integer comparison

2 views
Skip to first unread message

Loke

unread,
Aug 14, 2001, 6:08:19 AM8/14/01
to
Hello,

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!

bitro

unread,
Aug 14, 2001, 6:39:03 AM8/14/01
to

Uzytkownik "Loke" <reply-t...@please.com> napisal w wiadomosci
news:rM6e7.1022$Y53....@news1.oke.nextra.no...

> Suggestions?

if ( EndMonth < 10) {
SEndMonth = '0' + EndMonth;
} else {
SEndMonth = ''+ EndMonth;
}


Thor Larholm

unread,
Aug 14, 2001, 6:44:24 AM8/14/01
to
"bitro" <bi...@2com.pl> wrote in message news:9lav4m$kl3$1...@news.tpi.pl...

EndMonth = (EndMonth<10?'0':'') + EndMonth;

--
Thor Larholm
<URL: http://www.jibbering.com/faq/> FAQ for comp.lang.javascript


Steve van Dongen

unread,
Aug 14, 2001, 7:12:23 AM8/14/01
to

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

unread,
Aug 14, 2001, 7:12:00 AM8/14/01
to
if (EndMonth) < 10
EndMonth += '';
else

EndMonth = '0' + EndMonth;

--

Martin Honnen
http://javascript.faqts.com/
http://home.t-online.de/home/martin.honnen/jsgoddies.html


Hywel Jenkins

unread,
Aug 14, 2001, 1:22:21 PM8/14/01
to
In article <3B790780...@t-online.de>, Martin...@t-online.de
says...

> if (EndMonth) < 10
> EndMonth += '';
> else
> EndMonth = '0' + EndMonth;
Or rather
if (EndMonth < 10)

EndMonth = '0' + EndMonth;
--
Hywel I do not eat quiche
Web Site http://hyweljenkins.com/
MicroFAQ http://hyweljenkins.com/support/mfaq.htm

Dr John Stockton

unread,
Aug 14, 2001, 5:36:28 PM8/14/01
to
JRS: In article <rM6e7.1022$Y53....@news1.oke.nextra.no>, seen in
news:comp.lang.javascript, Loke <reply-t...@please.com> wrote at
Tue, 14 Aug 2001 12:08:19 :-

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

0 new messages