日期对象(Date)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var date = new Date(); //获取到当前的系统时间
document.write("年:"+ date.getFullYear()+"<br/>");//获取当前年份
document.write("月:"+ (date.getMonth()+1)+"<br/>");//getMonth+1才是当前月份
document.write("日:"+ date.getDate()+"<br/>");//当前月份的具体日期
document.write("日:"+ date.getDay()+"<br/>");//当前周几
document.write("时:"+ date.getHours()+"<br/>");
document.write("分:"+ date.getMinutes()+"<br/>");
document.write("秒:"+ date.getSeconds()+"<br/>");
//xxxx年yy月dd日 hh:mm:ss
//document.write("当前时间是:"+date.toLocaleString());格式固定2020/06/28 上午 03:03:60
document.write("当前时间是:"+date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日 "+
date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
</body>
</html>
显示当前时间:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
当前系统时间:<span id="time"></span>
</body>
<script type="text/javascript">
function getCurrentTime(){
//获取到当前的系统时间
var date = new Date();
//把当前系统时间拼装成我指定的格式。
var timeInfo = date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日 "+
date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
//找span对象
var spanObj = document.getElementById("time");
//设置span标签体的内容
spanObj.innerHTML = timeInfo.fontcolor("red");
}
getCurrentTime();
//定时方法.
window.setInterval("getCurrentTime()",1000); /* setInterval定时方法,第一个参数要指定调用的代码,需要时字符串形式的,第二参数是每隔指定的毫秒数调用指定的代码。*/
</script>
</html>
本文介绍了JavaScript中的Date对象,重点讲解如何使用它来显示当前时间。
2205

被折叠的 条评论
为什么被折叠?



