JavaScript学习笔记35-Date对象
2017-04-30 12:15
225 查看
前面简单介绍了Math对象,本文介绍Date对象,Date主要是获取日期和时间的一个接口。
我们先来看看打印一个当前时间,一秒打印一次当前时间。
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
<script type ="text/javascript">
function printTime(){
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var seconds = now.getSeconds();
document.write(hours+":"+mins+":"+seconds+"<br/>");
}
setInterval("printTime()",1000);
</script>
</body>
</html> 接下来,我们写一个显示年月日和时分秒,而且只在一个位置显示,不像前面显示多行。
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<div class="show_time">
<div id="showTime">显示时间的位置</div>
</div>
<script type ="text/javascript">
function printTime(){
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1; // JS获取的月份是从0开始
var day = now.getDate();
var hours = now.getHours();
var mins = now.getMinutes();
var seconds = now.getSeconds();
//document.write(hours+":"+mins+":"+seconds+"<br/>");
document.getElementById("showTime").innerHTML=""+year+"年"+month+"月"+day+"日 "+" "+hours+":"+mins+":"+seconds;
}
setInterval("printTime()",1000);
</script>
</body>
</html>
我们先来看看打印一个当前时间,一秒打印一次当前时间。
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
<script type ="text/javascript">
function printTime(){
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var seconds = now.getSeconds();
document.write(hours+":"+mins+":"+seconds+"<br/>");
}
setInterval("printTime()",1000);
</script>
</body>
</html> 接下来,我们写一个显示年月日和时分秒,而且只在一个位置显示,不像前面显示多行。
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<div class="show_time">
<div id="showTime">显示时间的位置</div>
</div>
<script type ="text/javascript">
function printTime(){
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1; // JS获取的月份是从0开始
var day = now.getDate();
var hours = now.getHours();
var mins = now.getMinutes();
var seconds = now.getSeconds();
//document.write(hours+":"+mins+":"+seconds+"<br/>");
document.getElementById("showTime").innerHTML=""+year+"年"+month+"月"+day+"日 "+" "+hours+":"+mins+":"+seconds;
}
setInterval("printTime()",1000);
</script>
</body>
</html>
相关文章推荐
- Javascript学习笔记(Date对象)
- 将未指定格式的日期字符串转化成java.util.Date类型日期对象
- Js Date对象
- java--jdk api中其他对象(System,Runtime,Calendar,Math,Random,Date)
- js的Date对象
- 对象函数asp获取和js中date对象getTime一样的数值
- JavaScript高级程序设计 读书笔记之十 本地对象Date日期
- 把给定的字符串解析为Date对象
- 黑马程序员——Java语言基础:其他对象(System、Runtime、Date、Calendar、Math)
- 本地创建Json对象时遇到 timestamp和date等对象时报异常
- JavaScript Date对象详解
- JS中的日期(Date)对象
- Date对象增加format函数
- Java基础18天--03--对象(Date)
- javascript-简单的日历实现及Date对象语法介绍(附图)
- Java实现的时间戳与date对象相互转换功能示例
- 黑马程序员之其他对象(System,Date,Calendar)
- JavaScript对象(一)之String对象和Date对象
- JS的Date对象在Safari与IOS中的兼容问题
- javascript中Date对象与函数