Page 1 of 1

javascript question

Posted: Thu Aug 05, 2004 6:39 am
by tores
Hi

How can i convert an integer = 2004 to 04 in javascript.

Regards tores

Posted: Thu Aug 05, 2004 7:47 am
by CoderGoblin
Try:

Code: Select all

<script language="javascript">
	my_num=2004;
	temp=my_num-2000;
	if (temp<0) &#123;
  		alert ('we have some form of error');
	&#125; else &#123;
		if (temp<10) &#123;
	  		new_str='0'+temp;
		&#125; 	else &#123;
	  		new_str=''+temp;
		&#125;
		document.write(new_str); 
	&#125;
</script>
Just a quick example. Probably neater ways of doing it for example you may wish to write a str_pad_left javascript function instead.