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

    时间是物理学七大常量之一。生活中记录时间有两种方式(或者说有两种计时系统):GMT(格林尼治时间)和UTC(协调世界时间)。

    一   创建Date对象

      JS中的Date对象只能通过new关键字创建。

    1 var now = new Date();

      需要注意的是:若将Date()作为常规函数调用(即不加 new 操作符),则只会返回一个表示当地时间的字符串(UTC还是GMT?取决于浏览器的实现。),而非 Date 对象。

    1 var now = Date();
    2 typeof now;//'string'

      另外,不像其他的 JavaScript 对象,Date 对象没有字面量语法。

      

      创建时间对象时可以传递参数:

      1、 不使用参数

    1 var now = new Date();//"Mon Aug 12 2019 16:00:16 GMT+0800 (中国标准时间)"

      

      2、 参数是一个表示时间的字符串

    1 var now = new Date(“2019-08-12”);
    2 //Thu Aug 12 2019 00:00:00 GMT+0800 (中国标准时间)
    3 var now1 = new Date(“2019/08/12”);
    4 //Thu Aug 12 2019 00:00:00 GMT+0800 (中国标准时间)

      

      3、 参数是年/月/日/时/分/秒/毫秒

      规则如下:

        a:年是必须的,月是从0开始,日是从1开始。

        b:如果月份超过11,则年份自动增加。

        c:如果日超过当月应有天数,则月自动增加

        d:时分秒毫秒均是类似。

        e:如果参数有缺省,则默认是0;

    1 var  date = new Date(2019,08,12,0,0,0,0);
    2 var date1 = new Date(2019,08,32);
    3 console.log(date);//Thu Sep 12 2019 00:00:00 GMT+0800 (中国标准时间)
    4 console.log(date1);//Wed Oct 02 2019 00:00:00 GMT+0800 (中国标准时间)

      

      4、 参数是一个数字

      数字表示1970-01-01 00:00:000至今的毫秒数。

    1 var now = new Date(1565600413336);
    2 console.log(now);//Mon Aug 12 2019 17:00:13 GMT+0800 (中国标准时间)

    二   Date对象的方法

      

      1、  get类

     1   //获取当前时间:
     2   var date = new date();//Mon Aug 12 2019 17:04:13 GMT+0800 (中国标准时间)
     3   //获取年:
     4   date.getFullYear();//2019
     5   //获取月: 
     6   date.getMonth();//07
     7   //获取日: 
     8   date.getDate();//12
     9   //获取时:
    10   date.getHours();//15
    11     //获取分:
    12    date.getMinutes();//4
    13   //获取秒:
    14   date.getSeconds();//13
    15   //获取毫秒:
    16   date.getMilliseconds()//445
    17    //获取1970-01-01 00:00:00至今的毫秒数:
    18    date.getTime();
    19 //1565600713445

      2、  set类

     1 //获取当前时间:
     2 var date = new date();//Mon Aug 12 2019 17:04:13 GMT+0800 (中国标准时间)
     3 //设置年:
     4 date.setFullYear(2018);
     5 date.getFullYear();//2018
     6 //设置月: 
     7 date.setMonth(6);
     8 date.getMonth();//6
     9 //设置日: 
    10 date.setDate(16);
    11 //设置时:
    12 date.setHours(14);
    13 //设置分:
    14 date.setMinutes(03);
    15 //设置秒:
    16 date.setSeconds(12);
    17 //设置毫秒:
    18 date.setMilliseconds(0);

      3、  获取时间的字符串表示

    1 var date = new Date();//Mon Aug 12 2019 17:22:55 GMT+0800 (中国标准时间)
    2 date.toString();//"Mon Aug 12 2019 17:22:55 GMT+0800 (中国标准时间)"
    3 date.toLocaleString();//"2019/8/12 下午5:22:55"
    4 date.toDateString();//"Mon Aug 12 2019"
    5 date.toLocaleDateString();//"2019/8/12"
    6 date.toTimeString();//"17:22:55 GMT+0800 (中国标准时间)"
    7 date.toLocaleTimeString();//"下午5:22:55"

    三   时间对象之间的计算

             两个时间对象相减,得到的是他们相差的毫秒数。

    1 var date = new Date();
    2 var newDate;
    3 setTimeout(function(){
    4     newDate = new Date();
    5     diff = newDate - date;
    6     console.log(diff);//5000
    7 },5000);//5s之后给newDate设置时间对象

      由于一些我们不可控原因,最终打印的值可能会在5000左右有一点偏差,但不会太大。

  • 相关阅读:
    Best Time to Buy and Sell Stock
    Remove Nth Node From End of List
    Unique Paths
    Swap Nodes in Pairs
    Convert Sorted Array to Binary Search Tree
    Populating Next Right Pointers in Each Node
    Maximum Subarray
    Climbing Stairs
    Unique Binary Search Trees
    Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/ruhaoren/p/11341511.html
Copyright © 2011-2022 走看看