欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JavaScript实现页面中显示距离2018年中秋节的天数

程序员文章站 2022-05-30 16:59:39
...
本文主要介绍使用JavaScript实现在页面中显示距离2018年中秋节的天数的相关资料,需要的朋友可以参考下,大家可以根据以下的代码思路进行实现自己的功能,希望能帮助到大家。

废话不多说了,具体代码如下所示:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>在页面中显示距离2018年中秋节的天数</title>
  <script type="text/javascript">
    function countdown(title,Intime,pId) {
      var online=new Date(Intime); //根据参数定义时间对象
      var now=new Date();   //定义当前系统时间
      var leave=online.getTime()-now.getTime(); //计算时间差
      var day=Math.floor(leave/(1000*60*60*24))+1;
      if (day>1){
          pId.innerHTML="<b>----距离"+title+"还有"+day+"天!<br/>" ; //页面显示信息
      }else{
        if (day==1){
            pId.innerHTML="<b>----明天就是"+title+"啦!<br/>" ; //页面显示信息
        }else{
          if (day==0) {
            pId.innerHTML = "<b>----今天就是" + title + "呀!<br/>"; //页面显示信息
          }else{
              pId.innerHTML="<b>----哎呀!"+title+"已经过了!<br/>" ; //页面显示信息
            }
          }
        }
    }
  </script>
</head>
<body>
  <table width="350" height="450" border="0" align="center" cellspacing="0" cellpadding="0">
    <tr>
      <td valign="bottom">
        <table width="346" height="418" border="0" align="center" cellspacing="0" cellpadding="0">
          <tr>
            <td width="76"></td>
            <td width="270">
              <p id="countDown">
                <b>---</b></p>
                <script type="text/javascript">
                  countdown("2018年中秋节","10/4/2018",countDown); //调用JavaScript函数
                </script>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>

相关推荐:

PHP计算日期间隔天数

JavaScript使用setInterval实现天数倒计时以及本地时间

你必须了解的JS获取日期相差天数的方法

以上就是JavaScript实现页面中显示距离2018年中秋节的天数的详细内容,更多请关注其它相关文章!