<div class="htmlarea"> <textarea id="runcode27087"> <title>如何显示当天日期(yyyy-mm-dd)的Javascript示例(用到Date对象的getFullYear,getMonth和getDate方法)</title> </textarea> <br><input onclick="runEx('runcode27087')" type="button" value="运行代码"><input onclick="doCopy('runcode27087')" type="button" value="复制代码"> <input onclick="doSave(runcode27087)" type="button" value="保存代码"> <a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">[Ctrl A 全选 注:</a>如需引入外部Js需刷新才能执行</div>]<br><script type="text/javascript"> var d = new Date() var vYear = d.getFullYear() var vMon = d.getMonth() + 1 var vDay = d.getDate() document.write(vYear) document.write("-") document.write(vMon<10 ? "0" + vMon : vMon) document.write("-") document.write(vDay<10 ? "0"+ vDay : vDay ) </script>三元运算符..vMon如果小于10就输出"0" vDay,否则输出vDay