Click to See Complete Forum and Search --> : javascript equiv to right(var,1)


khaki
03-30-2003, 03:30 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

gil davis
03-30-2003, 03:53 PM
<script>
var now = new Date();
var m = now.getMinutes() + "";
alert(m.substr(m.length - 1));
</script>The trick is to convert the number to a string.

khaki
03-30-2003, 04:21 PM
oh Gil...
that's it!
Thank you sooo much!

I finally did figure out that I needed to convert the value to a string... but I would be embarrassed to show you what convoluted means I was coming up with to get it to return only the right-most character.
Yikes!

document.write(m.substr(m.length - 1));
is not an intuitive answer for someone who thinks like this:
document.write(right(m,1));

... and I have to admit... I still don't understand the "logic" of that (i was under the impression that there were 2 parameters required for that method)... so I'll have to play around with it a little to make sure that I understand how to control it for future use.

Thanks again!
k

mutus
03-30-2003, 07:35 PM
for what your doing here (grabbing the last digit of m) there's no need to convert to string:

var now= new Date();
var m = now.getMinutes();
document.write(m%10);

khaki
03-30-2003, 07:44 PM
simply perfect!

Thanks mutus!!!

;) k