var d, s; d = new Date(); s = d.getYear() + "-"; s = s + (d.getMonth()+1) + "-"; s = s + d.getDate(); 该程序得到的是例如:2002-1-9 但是我想得到格式为:2002-01-09格式的数字该怎么办呢?
function GetDate(){
var d, s,m,nd;
d = new Date();
s = d.getYear();
m =d.getMonth()+1;
if (m<10){
m="0"+m;
}
nd =d.getDate();
if (nd<10){
nd="0"+nd;
}
s=s+"-"+m+"-"+nd;
alert(s);
}
<script>
var d, s;
d = new Date();
s = d.getYear() + "-";
if(d.getMonth()+1 < 10)
s = s + "0" + (d.getMonth()+1) + "-";
else
s = s + (d.getMonth()+1) + "-";
if(d.getDate()<10)
s = s + "0" + d.getDate();
else
s = s + d.getDate();
alert(s)
</script>