zoukankan      html  css  js  c++  java
  • JavaScript之Date对象

    Date对象 (日期对象)

     

    1. 基本使用

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     6     <title>Document</title>
     7 </head>
     8 <body>
     9     <script>
    10 
    11         //无参 ,则返回的是当前的系统的日期时间
    12         //Sat Apr 25 2020 19:39:03 GMT+0800 (中国标准时间)
    13         var date01  = new Date();
    14         console.log(date01);
    15 
    16         //有参,则返回的是参数里面的时间
    17         var date02 = new Date("2011-10-01 08:08:08");
    18         console.log(date02);
    19     </script>
    20 </body>
    21 </html>

    2.格式化日期

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     6     <title>Document</title>
     7 </head>
     8 <body>
     9     <script>
    10         var date01 = new Date();
    11 
    12         //先获取一下当前日期
    13         console.log(date01);
    14         
    15 
    16         //getFullYear()  返回年份
    17         console.log(date01.getFullYear());
    18 
    19         //getMonth()  返回月份,由于只有 0 - 11月,所以一般会 加1
    20         console.log(date01.getMonth()+1);
    21         
    22         //getDate()  返回当前是几号
    23         console.log(date01.getDate());
    24 
    25         //getDay()    返回是周几,不过周日返回的是0
    26         var array = ["周日","周一","周二","周三","周四","周五","周六"];
    27         console.log(date01.getDay());
    28         console.log(array[date01.getDay()]);
    29 
    30         //getHours()   返回小时
    31         console.log(date01.getHours());
    32 
    33         //getMinutes()   返回分钟
    34         console.log(date01.getMinutes());
    35 
    36         //getSeconds()   返回秒数
    37         console.log(date01.getSeconds());
    38         
    39         
    40         
    41         
    42         
    43     </script>
    44 </body>
    45 </html>

    3. 获取日期的毫秒数(当前时间距离1970年1月1日的总的毫秒数)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <script>
         //先获取当前时间的日期 var date01 = new Date();
          //再通过 getTime()获取总的毫秒数
    var time01 = date01.getTime(); console.log(time01); </script> </body> </html>

     

  • 相关阅读:
    避免多次提交
    Django 10
    Django 08
    Django 07
    Django 06
    Django 05
    Django 04
    Django 03
    Django 02
    Django 01
  • 原文地址:https://www.cnblogs.com/zysfx/p/12774994.html
Copyright © 2011-2022 走看看