网站头部动态日期--JS实现

本文介绍了一段使用JavaScript编写的代码,该代码可以根据当前时间显示不同的问候语,并且能够显示完整的日期格式,包括年月日及星期几。通过简单的HTML展示,实现了日期和问候语的动态展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script language='javascript'>
today
=new Date();
var day; var date; var hello; var wel;

hour
=new Date().getHours()

if(hour < 6)hello='凌晨好'
else if(hour < 9)hello='早上好!'
else if(hour < 12)hello='上午好!'
else if(hour < 14)hello='中午好!'
else if(hour < 17)hello='下午好!'
else if(hour < 19)hello='傍晚好!'
else if(hour < 22)hello='晚上好!'
else {hello='夜里好!'}

if(today.getDay()==0)day='星期日'
else if(today.getDay()==1)day='星期一'
else if(today.getDay()==2)day='星期二'
else if(today.getDay()==3)day='星期三'
else if(today.getDay()==4)day='星期四'
else if(today.getDay()==5)day='星期五'
else if(today.getDay()==6)day='星期六'

date
=(today.getYear())+''+(today.getMonth() + 1 )+''+today.getDate()+'';

if(hour<1)wel='子时';
else if(hour<3)wel='丑时';
else if(hour<5)wel='寅时';
else if(hour<7)wel='卯时';
else if(hour<9)wel='辰时';
else if(hour<11)wel='巳时';
else if(hour<13)wel='午时';
else if(hour<15)wel='未时';
else if(hour<17)wel='申时';
else if(hour<19)wel='酉时';
else if(hour<21)wel='戌时';
else if(hour<23)wel='亥时';
else {wel='子时';}
</script>

 测试效果:<table border="0" width="120" cellspacing="0" cellpadding="0">
 <tr height="19" align="center">
  <td>
  <script>document.write("大家,"+hello);</script>
  </td>
 </tr>
 
 <tr height="19"  align="center">
  <td style="border-bottom:#000000 solid 1px;padding:0 0 2 0;">
  <SCRIPT>document.write(date+day)</SCRIPT>
  </td>
 </tr>
 <tr height="19"  align="center">
  <td style="padding:0 0 4 0;">www.lrn.cn</td>
 </tr>
</table>

把JS代码与测试代码写到同一个HTML页面中运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值