PDA

View Full Version : javascript equiv to right(var,1)


khaki
03-30-2003, 09:23 PM
Hi everyone...

I am trying to get the last portion of the current minute.

So far I have:

var now= new Date();
var m = now.getMinutes();


What I want to do in Javascript is the VB equivalent of:

right(m,1)

(the first character in the string from the right)

is there a Javascript equivalent for this?

I've been trying to play with substring() and substr() and slice(), but I'm getting only errors (... and frustrated... in that order. lol)

Ideas?

once again arguing with Javascript... and losing as usual...
k

khaki
03-30-2003, 10:25 PM
okay...
never mind. I got it:

var now = new Date();
var m = now.getMinutes() + "";
document.write(m.substr(m.length - 1));

(for those who are interested)


document.write(right(m,1)); seems a lot less messy... but there's no sense in fighting it.
k

boywonder
03-30-2003, 10:38 PM
For what it's worth, I would probably do something like this to get the last digit and avoid strings. Maybe this could work for you

var now = new Date();
var m = now.getMinutes();

var lastnum = m<10 ? m : m%10;
document.write(lastnum);

khaki
03-30-2003, 10:55 PM
yeah.... that's what I'm talkin' about!

Excellent!

I was wondering if there was an easy way to get that last digit all by itself.

Thanks boywonder!
Gotham City (and the surrounding NJ area) are much safer now (wink).
Thanks.
;) k

joh6nn
03-30-2003, 11:16 PM
how 'bout

var now = new Date();
var m = String(now.getMinutes());
alert(m.charAt(--m.length));