该例子输出当天为这个月的哪一天:
<script language="JavaScript"> var d = new Date(); document.write( d.getDate() ); </script>
运行该例子,输出:
4
该例子将年月日按照不同格式来显示:
<script language="JavaScript"> var d = new Date(); var year = d.getFullYear(); var month = d.getMonth() + 1; var day = d.getDate(); document.write(year + "-" + month + "-" + day + "<br />"); document.write(year + "." + month + "." + day + "<br />"); document.write(year + "/" + month + "/" + day + "<br />"); document.write(year + "年" + month + "月" + day + "日"); </script>
运行该例子,输出:
2012-3-4 2012.3.4 2012/3/4 2012年3月4日