HTML网页中实现获利当前日期的方式一般使用JavaScript。
详细方式:网页中或JS文件中加入以下方法:
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-" ;
var seperator2 = ":" ;
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
alert(currentdate);
} |
在需要 调用的地方运行该函数,如要弹出当前时间,则:
<a href= "javascript:void(0)" onclick= "getNowFormatDate()" >点我</a>
</body> |
完整代码如下:
<!doctype html>
<
html
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>无标题文档</
title
>
</
head
>
<
body
>
<
script
type
=
"text/javascript"
>
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate
;
}
</
script
>
<
a
href
=
"javascript:void(0)"
onclick
=
"getNowFormatDate()"
>点我查看当前时间</
a
>
// 可以用javascript,在html的head里面写如下代码
<script language=
"javascript"
>
var
now=
new
Date();
document.write(
"今天是"
+now.year()+
"年"
+now.month()+
"月"
+now.day()+“日”);
</script>