如果让你写个js获取当前日期的方法,你会怎么写?会写成下面这样吗?
<script type="text/javascript">
var curDate = new Date();
var curDateStr = curDate.getFullYear() + "-" + curDate.getMonth() + "-" + curDate.getDay();
alert(curDateStr);
</script>
var curDate = new Date();
var curDateStr = curDate.getFullYear() + "-" + curDate.getMonth() + "-" + curDate.getDay();
alert(curDateStr);
</script>
如果真是这样你就错了,不信可以运行下试试!
正确的做法请看下列代码:
View Code
<script type="text/javascript">
var curDate = new Date();
var curDateStr = curDate.getFullYear() + "-" + (curDate.getMonth() + 1) + "-" + curDate.getDate();
alert(curDateStr);
</script>
var curDate = new Date();
var curDateStr = curDate.getFullYear() + "-" + (curDate.getMonth() + 1) + "-" + curDate.getDate();
alert(curDateStr);
</script>