zoukankan      html  css  js  c++  java
  • JS的内置对象Date

    内置对象Date:

        Date 对象在实际开发中,使用得很频繁,且容易在细节地方出错,需要引起重视。
        用来处理日期和时间,与Math 对象不同,Date 对象是一个 构造函数 ,需要先 实例化 才可使用
     
    创建Date 对象:
    如果Date()不写参数,就返回当前时间对象
    var date1 = new Date();
                console.log(date1);
                console.log(typeof date1);
    如果Date()里面写参数,就会返回括号里输入的时间对象
    var date21 = new Date('2020/02/17 21:00:00');
                console.log(date21);
                // Mon Feb 17 2020 21:00:00 GMT+0800 (中国标准时间)
    
                var date22 = new Date(2020, 2, 18); // 第二个参数返回的是三月,不是二月
                console.log(date22);
                // Wed Mar 18 2020 00:00:00 GMT+0800 (中国标准时间)
    
                var date23 = new Date(2020, 3, 18, 22, 59, 58);
                console.log(date23);
                // Sat Apr 18 2020 22:59:58 GMT+0800 (中国标准时间)
    
                var date24 = new Date('2020/04/19'); // 返回的就是四月
                console.log(date24);
                // Sun Apr 19 2020 00:00:00 GMT+0800 (中国标准时间)
    
                var date25 = new Date('2020-05-20');
                console.log(date25);
                // Wed May 20 2020 08:00:00 GMT+0800 (中国标准时间)
    
                var date26 = new Date('Wed Jan 27 2017 12:00:00 GMT+0800 (中国标准时间)');
                console.log(date26);
                // Fri Jan 27 2017 12:00:00 GMT+0800 (中国标准时间)
    日期的格式化:效果不直观,获取日期指定部分,先了解对象的方法
     
     
    Date对象的方法:
            getMonth() :获取月0-11  0代表1月
            getDate() :获取日1-31  获取的是几号
            getDay() :获取星期0-6   0代表周日,1代表周一
            getFullYear() :获取年份
     
      
    年月日的格式化:
     
    获取时间戳:
    getTime()
      时间戳:指的是从格林威治标准时间的1970年1月1日,0时0分0秒到当前日期所花费的毫秒数(1秒 = 1000毫秒)。
            计算机底层在保存时间时,使用的都是时间戳。时间戳的存在,就是为了统一时间的单位。
            我们经常会利用时间戳来计算时间,因为它更精确。而且,在实战开发中,接口返回给前端的日期数据,都是以时间戳的形式。
     
    获取 Date 对象的时间戳
    1.    var date1 = +new Date();
                console.log(date2); 
    
            2.    var date2 = new Date();
                console.log(date2.getTime());
            
    获取当前时间的时间戳
    console.log(Date.now());      //不兼容低版本的IE

      

    利用时间戳检测代码的执行时间
                前面定义 时间戳1,在业务代码的后面定义 时间戳2。
                把这两个时间戳相减,就能得出业务代码的执行时间。
     
  • 相关阅读:
    人月神话2
    cJson 常见用法
    Python C扩展
    动态链接--运行时加载dlopen
    mtrace 简介
    Linux coredump
    动态链接--so的搜索过程
    线程同步--条件变量
    编译过程学习
    Linux 信号
  • 原文地址:https://www.cnblogs.com/yunhai666/p/13039272.html
Copyright © 2011-2022 走看看