js获取时间

获取与展示当前时间及日期的JavaScript实现

1、date("Y-m-d H:i:s");//php中用一句话就能获取当前显示时间
2、js中获取当前时间:
<script language="javaScript">
var now= new Date( );
var hour = now.getHours( );
if (hour>=0 && hour <=12)
 document.write("上午好!");
if (hour>12 && hour<= 18)
    document.write("下午好!");
if (hour>18 && hour <24)
    document.write("晚上好!");
document.write("<P>今天日期:"+now.getFullYear()+"年"
        +(now.getMonth( )+1)+"月"+now.getDate()+"日");
document.write("<P>现在时间:"+now.getHours()+"点"+now.getMinutes( )+"分");
</script>

3、利用setTimeout("time()",1000)让时间走起来,这个与上面的区别是上面的是获取当前的时间,时间不在走动,这个是它一直走,没有停
<script language="javascript">
function time()
{for(i=0;i<=10000;i++)
for(j=0;j<=100;j++)

var now=new Date();
var hour=now.getHours();
var hel;
document.form1.y.value=now.getFullYear();
document.form1.m.value=now.getMonth( );
document.form1.date.value=now.getDate();
document.form1.day.value=now.getDay();
document.form1.h.value=now.getHours();
document.form1.mi.value=now.getMinutes();
document.form1.sec.value=now.getSeconds();

if(hour>=6&&hour<12) hel="上午好!";
if(hour>=12&&hour<18)hel="下午好!";
if((hour>=18&&hour<=23)||(hour>=0&&hour<6)) hel="晚上好!" ;
document.form1.hello.value=hel;
 setTimeout("time()",1000);
 }
</script>
</head>

<body onload="time()">

<form name="form1">
大家:<input name="hello" type="text"><br>
今天是<input name="y" type="text">年
<input name="m" type="text">月<input name="date" type="text">日星期<input name="day"

type="text"><input name="h" type="text">时<input name="mi" type="text">分<input name="sec"

type="text">秒
</form>

</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值