Sep
10
Javascript leading zero string prototype
When you are working with dates it is useful to have all dates formatted in a same way. For example, it is better to have all dates formatted like ‘2007-01-01′ then like ‘2007-1-1′. This is javascript prototype that can help you with this leading zeros:
<script type="text/javascript">
String.prototype.leadingZero = function(){
if (this.length == 1) return '0' + this; else return this;
}
//show how it works
test1 = '1'; // we need zero here
test2 = '02'; // don't need zero
document.write ('"' + test1.leadingZero() + '"<br>');
document.write ('"' + test2.leadingZero() + '"<br>');
</script>
</body>
</html>
No Comments
Make A CommentNo comments yet.
Comments RSS Feed TrackBack URL

