日期:2014-05-16  浏览次数:20453 次

请教javascript或者java自动生成序号的代码
要求javascript自动生成固定格式的序号   以便往数据库里添加   如“SBBJ-20070300001”

SBBJ-   为固定格式   2007为年度   03为月份   000001为自动增长的序号。

java代码也可以。

谢谢了!

------解决方案--------------------
<script>
var base = 99;
var long = 10;
var date = new Date();
var year = date.getYear();
var month = date.getMonth() + 1;
var len = 5;
var tmp = " "
var s = " ";
var str = "SBBJ- "
str += year.toString();
str += month.toString().length < 2 ? "0 "+month.toString() : month.toString();
for(var i=0; i < long; i++){
  while((tmp+base.toString()).length < len){tmp += "0 ";}
  s = str+tmp+base.toString();
  document.write(s+ " <br> ");
  tmp = " ";
  base += 1;
}
</script>

------解决方案--------------------
<script>
var now=new Date();
var month = now.getMonth()+1;
var str = "SBBJ- "+now.getYear();
str+=month <10? "0 "+month:month;
var zero = "00000 ";
for(i=1;i <20;i++){
var tempStr = str;
var number = i.toString();
tempStr+= zero.substring(0,zero.length-number.length)+number;
document.write(tempStr+ " <br/> \n ");
}
</script>