JavaScript Date对象几个实例

JavaScript 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
p {
    color:#09F;
}
</style>
</head>

<body onload="time()">
<p>此刻时间:</p>
<div id="time"></div>
<script>
function time()
{
    var d=new Date();
    var h=d.getHours();
    var m=d.getMinutes();
    var s=d.getSeconds();
    m=checkTime(m);
    s=checkTime(s);
    document.getElementById(time).innerHTML=h+":"+m+":"+s;
    var t=setTimeout(time(),500);
    
    function checkTime(i)
    {
        if(i<10)
        {i="0"+i;}
        return i;}
}
</script>
<p>现在的时间是:</p>
<script>
document.write(Date());
</script>
<p>从1970/01/01过去的毫秒:</p>
<script>
var d=new Date();
document.write(d.getTime());
</script>
<p>设定年份:</p>
<script>
var d=new Date();
d.setFullYear(2014,2,22);//注意:表示月份的参数介于 0 到 11 之间。也就是说,如果希望把月设置为 8 月,则参数应该是 7。
document.write(d);
</script>
<p>日期转换为UTC字符串</p>
<script>
var d=new Date();
var s=d.toUTCString();
document.write(s);
</script>
<p>getDay()</p>
<script>
var d=new Date();
document.write(d.getDay());
</script>
<p>今天是星期?</p>
<script>
var array=new Array("星期日7","星期一","星期二","星期三","星期四","星期五","星期六");
var d=new Date();
document.write(array[d.getDay()]);
</script>
<p>将日期设置为500天之后</p>
<script>
var myDate=new Date();
document.write("现在:"+myDate+<br>);
myDate.setDate(myDate.getDate()+500);
document.write("500五天后:"+myDate);
</script>
<p>日期比较:</p>
<script>
var d=new Date();
d.setFullYear(2008,7,8);
var today=new Date();
if(today>d)
{document.write("2008北京奥运会已开幕");}
else
{document.write("2008北京奥运会待开幕");}
</script>
</body>
</html>
JavaScript Date对象几个实例

JavaScript Date对象几个实例

上一篇:Linux命令之命令别名


下一篇:LInux系统下搭建redis集群