• 一个简单的 js 时间对象创建


    JS中获取时间很常见,凑凑热闹,也获取一个时间对象试试

    首先,先了解js的获取时间函数如下:

    var myDate = new Date();          //创建一个时间对象

    myDate.getYear();                      // 获取当前年份(2位)

    myDate.getFullYear();                // 获取当前完整的年份(4位,1970----???)

    myDate.getMonth();                   // 获取当前月份(0--11,   0 代表1月)

    myDate.getDate();                      // 获取当前是哪一日 (1----31)

    myDate.getDay();                       // 获取当前是哪一天,即星期几(0---6 ,0代表星期天)

    myDate.getTime();                      // 获取当前时间的毫秒数(从1970.1.1开始计算)

    myDate.getHours();                    // 获取当前时间的小时数(0---23)

    myDate.getMinutes();                  //获取当前时间的分钟数 (0---59)

    myDate.getSeconds();                // 获取当前时间的秒数 (0---59)

    myDate.getMilliseconds();           // 获取当前时间的毫秒数 (0---999)

    myDate.toLocaleDateString();     // 获取当前时间的日期

    myDate.toLocaleTimeString();     // 获取当前时间

    myDate.toLocaleString();             //获取当前日期与时间

    创建一个时间如下:

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4   <meta charset="UTF-8">
     5   <title>Document</title>
     6   <style>
     7     .box{
     8       margin:200px auto;
     9       width:400px;
    10     }
    11     span,code{
    12        display: inline-block;
    13        font-size: 75px;
    14     }
    15   </style>
    16   <script>
    17       window.onload = function(){
    18 
    19         function OTime(){
    20           this.aspan = document.querySelectorAll('.box span');
    21         };
    22         OTime.prototype.todouble = function( n ){
    23           return n>10 ? '' + n : '0'+ n ;
    24         };
    25         OTime.prototype.getcurtime = function(){
    26           var odate = new Date();
    27           var OHour = odate.getHours();
    28           var OMin = odate.getMinutes();
    29           var OSec = odate.getSeconds();
    30           return this.todouble( OHour ) + this.todouble( OMin ) + this.todouble( OSec ) ;
    31         };
    32         OTime.prototype.setcurTime = function(){
    33           var str = this.getcurtime();
    34           for(var i=0;i<this.aspan.length;i++){
    35             this.aspan[i].innerHTML = str.charAt( i );
    36           };
    37         };
    38         OTime.prototype.showtime = function(){
    39           this.setcurTime();
    40           var that = this ;
    41           setInterval(function(){
    42             that.setcurTime();
    43           },1000);
    44         };
    45         var curtime = new OTime();
    46         curtime.showtime();
    47 
    48       };
    49   </script>
    50 </head>
    51 <body>
    52   <div class="box">
    53       <span>2</span>
    54       <span>2</span>
    55       <code>:</code>
    56       <span>1</span>
    57       <span>1</span>
    58       <code>:</code>
    59       <span>3</span>
    60       <span>3</span>
    61   </div>
    62 </body>
    63 </html>

    运行结果:

    http://www.cnblogs.com/huanying2015 博客随笔大多数文章均属原创,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
  • 相关阅读:
    54、操控变形—调整
    leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树(中等)
    leetcode 219. Contains Duplicate II 存在重复元素 II(简单)
    leetcode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 (中等)
    leetcode 530. Minimum Absolute Difference in BST二叉搜索树的最小绝对差 (简单)
    leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal 根据前序和后
    leetcode 146. LRU Cache LRU 缓存 (简单)
    leetcode 785. Is Graph Bipartite判断二分图 (中等)
    leetcode 94. Binary Tree Inorder Traversal 二叉树的中序遍历(中等)
    leetcode 220. Contains Duplicate III 存在重复元素 III(困难)
  • 原文地址:https://www.cnblogs.com/huanying2015/p/8018407.html
Copyright © 2020-2023  润新知