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

JavaScript Date对象应用实例分享

程序员文章站 2022-04-09 21:28:43
本文实例为大家分享了js date对象应用3个实例,供大家参考,具体内容如下 一.获取日期时间,秒数实时跳动 ...

本文实例为大家分享了js date对象应用3个实例,供大家参考,具体内容如下

一.获取日期时间,秒数实时跳动

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>date01</title>
  <style>
    #date{
      position: absolute;
      font-size: 30px;
      font-family: '微软雅黑';
      top:50%;
      left:50%;
      -webkit-transform:translate(-50%,-50%);
    }
  </style>
</head>
<body>
  <div id="date">2017.10.29</div>
  <script>
    window.onload = function(){
      showtime();
    }
    //checktime(i)对分钟和秒进行判断,如果小于10,在其前补0.
    function checktime(i){
      return i < 10? "0"+i:i;
    }
    function showtime(){
      var time = new date();//日期对象
      console.log("new date :"+time);
      var year_1 = time.getyear();//获得年份
      console.log("getyear(): "+year_1);
      var year_2 = time.getfullyear();//获得完整年份
      console.log("getfullyear(): "+year_2);
      var month = time.getmonth();//获得月份(0~11)
      console.log("getmonth(): "+month);
      var date = time.getdate();//获得日期(1~31)
      console.log("getdate(): "+date);
      var day = time.getday();//星期几(0~6)
      console.log("getday(): "+day)
      var hours = time.gethours();//小时(0~23)
      console.log("gethours(): "+hours);
      var min = time.getminutes();//分钟(0~59)
      console.log("getminutes(): "+min);
      var seconds = time.getseconds();//秒
      console.log("getseconds(): "+seconds);

      min = checktime(min);
      seconds = checktime(seconds);

      var weekday = new array(7);
      weekday[0] = "星期天"; 
      weekday[1] = "星期一";
      weekday[2] = "星期二";
      weekday[3] = "星期三";
      weekday[4] = "星期四";
      weekday[5] = "星期五";
      weekday[6] = "星期六";

      var str_time = year_2 + "年" + (month+1) + "月" + date + "日" +" "+ weekday[day] +" "+ hours +":" + min +":" + seconds;

      document.getelementbyid('date').innerhtml = str_time;
      settimeout(showtime,500);//每500ms执行一次,实现秒数实时跳动。
    }  
    
  </script>
</body>
</html>

二.高考倒计时功能(天)

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>date02</title>
  <style>
    #date{
      position: absolute;
      font-size: 30px;
      font-family: '微软雅黑';
      top:50%;
      left:50%;
      -webkit-transform:translate(-50%,-50%);
    }
  </style>
</head>
<body>
  <div id="date">2017.10.29</div>
  <script>
    window.onload = function(){
      var now = new date();//获取当前时间
      var timedate = new date("2018,12,23");//截止时间
      //gettime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
      var date = timedate.gettime() - now.gettime();
      //math.ceil()向上取整
      var time = math.ceil(date/(24*60*60*1000));
      document.getelementbyid('date').innerhtml = time;

    }
  </script>
</body>
</html>

三.限时抢

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>date03</title>
  <style>
    #date{
      position: absolute;
      font-size: 30px;
      font-family: '微软雅黑';
      top:50%;
      left:50%;
      -webkit-transform:translate(-50%,-50%);
    }
  </style>
</head>
<body>
  <div id="date">2017.10.29</div>
  <script>
    window.onload = function(){
      showtime();

    }
    function checktime(i){
      return i < 10? "0"+i:i;
    }
    function showtime(){
      var now = new date();//获取当前时间
      var timedate = new date("2017/10/30,24:00:00");//截止时间
      //gettime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
      var lefttime = (timedate.gettime() - now.gettime())/1000;
      var d = checktime(parseint(lefttime/(24*60*60)));
      var h = checktime(parseint(lefttime/(60*60)%24));
      var m = checktime(parseint(lefttime/60%60));
      var s = checktime(parseint(lefttime%60));
      var time = d + "天" + h + "小时" + m + "分钟" + s + "秒";
      document.getelementbyid('date').innerhtml = time;
      settimeout(showtime,500);
    }
  </script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。