网页中通过JavaScript 获取当前系统日期和时间

< script language = " JavaScript " >
// 定义一个tick函数,以获取系统的时间
function  tick()
{
var year,month,day,hours,minutes,seconds,ap;
var intYear,intMonth,intDay,intHours,intMinutes,intSeconds;
var today;
today
=new Date();
intYear
=today.getYear();
intMonth
=today.getMonth()+1;
intDay
=today.getDate();
intHours
=today.getHours();
intMinutes
=today.getMinutes();
intSeconds
=today.getSeconds();
//获取系统时间的小时数
if(intHours==0)
{
hours
=intHours+":";
ap
="凌晨";
}

else if(intHours<12)
{
hours
=intHours+":";
ap
="早晨";
}

else if(intHours==12)
{
hours
=intHours+":";
ap
="中午";
}

else
{
intHours
=intHours-12;
hours
=intHours+":";
ap
="下午";
}

//获取系统时间的分数
if(intMinutes<10)
{
minutes
="0"+intMinutes+":";
}

else
minutes
=intMinutes+":";
//获取系统时间的秒数
if(intSeconds<10)
seconds
="0"+intSeconds+" ";
else
seconds
=intSeconds+" ";
timeString
=year+month+day+hours+minutes+seconds+ap;
Clock.innerHTML
=timeString;
//每隔0.1秒钟执行一次tick函数
window.setTimeout("tick()",100);
}

window.onload
= tick;
</ script >

下面这句是调用函数
第一种方式:
< div id = " Clock " ></ div >
   第二种方式:
< div id = " jb51 " ></ div >
< script > setInterval( " document.getElementById('jb51').innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay()); " , 1000 );
</ script >
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值