function DateDemo(){ var d, s = "Today's date is: "; // 声明变量。 d = new Date(); // 创建 Date 对象。 s += (d.getMonth() + 1) + "/"; // 获取月份。 s += d.getDate() + "/"; // 获取日。 s += d.getYear(); // 获取年份。 return(s); // 返回日期。 }
<SCRIPT language="javascript"> var aDate=new Date(); var str=aDate.getFullYear()+"年"; str+=aDate.getMonth()+1+"月"; str+=aDate.getDate()+"日"; str+=aDate.getHours()+"时"; str+=aDate.getMinutes()+"分"; str+=aDate.getSeconds()+"秒"; alert(str); </SCRIPT>
但我们获取时间时一般获取的是服务器时间,因为代码运行在服务器端。
如果要获取客户端时间就用JS客户端脚本来完成。有很多静态网页中的时间就是用JS来获取客户端的时间。
var d, s = "Today's date is: "; // 声明变量。
d = new Date(); // 创建 Date 对象。
s += (d.getMonth() + 1) + "/"; // 获取月份。
s += d.getDate() + "/"; // 获取日。
s += d.getYear(); // 获取年份。
return(s); // 返回日期。
}
var aDate=new Date();
var str=aDate.getFullYear()+"年";
str+=aDate.getMonth()+1+"月";
str+=aDate.getDate()+"日";
str+=aDate.getHours()+"时";
str+=aDate.getMinutes()+"分";
str+=aDate.getSeconds()+"秒";
alert(str);
</SCRIPT>