JS获取日期yy-mm-dd格式

时间:2014-05-09 18:28:18   收藏:0   阅读:784

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var myDate=new Date();
var month = myDate.getMonth() + 1;
var Day = myDate.getDate();
var today = "";
if(month<10){
if(Day<10){
today = myDate.getFullYear() + "-0" + month + "-0" + Day;
}else{
today = myDate.getFullYear() + "-0" + month + "-" + Day;
}
}
else{
if(Day<10){
today = myDate.getFullYear() + "-" + month + "-0" + Day;
}else{
today = myDate.getFullYear() + "-" + month + "-" + Day;
}
}

方法二:

1
2
3
function appendZero(s){return ("00"+ s).substr((s+"").length);}  //补0函数
var d = new Date();
alert(d.getFullYear() + "-" + appendZero(d.getMonth() + 1) + "-" + appendZero(d.getDate()));

  

JS获取日期yy-mm-dd格式,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!