• 【JavaScript】Date对象(创建时间对象、常用Date方法总结)


    🐚作者简介:苏凉(专注于网络爬虫,数据分析,正在学习前端的路上)
    🐳博客主页:苏凉.py的博客
    🌐系列总专栏:web前端基础教程
    👑名言警句:海阔凭鱼跃,天高任鸟飞。
    📰要是觉得博主文章写的不错的话,还望大家三连支持一下呀!!!
    👉关注✨点赞👍收藏📂

    每日推荐

    给大家推荐一款神器。无论你是学生党还是上班族都可以使用,这里涵盖了面试题库,在线刷题,各个大厂的面试/笔试真题等。如果你还是学生,最重要的一点就是模拟面试功能,智能AI1v1面试,帮助你早日拿到大厂offer!点击链接即可直达!=>牛客网-找工作神器
    在这里插入图片描述

    在js中使用Date对象表示时间

    创建一个时间对象

    直接使用构造函数创建一个时间对象,则返回的是当前代码执行的时间。

    var time = new Date();
    console.log(time);
    //Fri Jul 01 2022 11:04:35 GMT+0800 (中国标准时间)
    
    • 1
    • 2
    • 3

    创建一个自定义时间对象

    要创建一个自定义时间对象时,需要在构造函数中传递一个时间字符串做为参数。
    日期格式为:月/日/年 时:分:秒

    var time2 = new Date('6/7/2019 8:00:00');
    console.log(time2)
    //Fri Jun 07 2019 08:00:00 GMT+0800 (中国标准时间)
    
    • 1
    • 2
    • 3

    Date方法

    getDate() 获取天

    返回当前Date对象中的一个月的第几天。

    var time1 = new Date();
    console.log(time1);
    //Fri Jul 01 2022 11:18:49 GMT+0800 (中国标准时间)
    console.log(time1.getDate());
    //1
    
    • 1
    • 2
    • 3
    • 4
    • 5

    getDay() 获取周几

    返回当前Date对象中的星期几。
    注意:当时间为周日时返回0。即1-6表示周一到周六,0表示周日。

    var time1 = new Date();
    console.log(time1);
    //Fri Jul 01 2022 11:18:49 GMT+0800 (中国标准时间)
    console.log(time1.getDay())
    //5
    
    • 1
    • 2
    • 3
    • 4
    • 5

    getMonth() 获取月份

    返回当前Date对象中的月份。
    注意:返回月份时返回的数在0-11区间表示1月到12月,0表示1月,以此类推。

    var time1 = new Date();
    console.log(time1);
    //Fri Jul 01 2022 11:18:49 GMT+0800 (中国标准时间)
    console.log(time1.getMonth());
    //6
    
    • 1
    • 2
    • 3
    • 4
    • 5

    getFullYear() 获取年份

    返回当前Date对象中的年份。

    var time1 = new Date();
    console.log(time1);
    //Fri Jul 01 2022 11:18:49 GMT+0800 (中国标准时间)
    console.log(time1.getFullYear());
    //2022
    
    • 1
    • 2
    • 3
    • 4
    • 5

    获取时/分/秒/毫秒

    getHours()返回当前Date对象的小时数
    getMinutes()返回分钟数
    getSeconds()返回秒数
    getMilliseconds()返回毫秒数

    var time1 = new Date();
    console.log(time1);
    //Fri Jul 01 2022 11:40:20 GMT+0800 (中国标准时间)
    console.log(time1.getHours()); //返回小时
    //11
    console.log(time1.getMinutes()); //返回分钟
    //40
    console.log(time1.getSeconds()); //返回秒
    //20
    console.log(time1.getMilliseconds()); //返回毫秒
    //893
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    getTime() 获取时间戳

    时间戳指的是从格林威治标准时间1970年1月1日0时0分0秒当前日期所花费的毫秒数(1秒=1000毫秒)。
    因为时间的相互转换特别麻烦,因此计算机底层保存时间都是用时间戳来保存的。

    var time1 = new Date();
    console.log(time1);
    //Fri Jul 01 2022 11:54:40 GMT+0800 (中国标准时间)
    console.log(time1.getTime());
    //1656647680176
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Date.now() 获取当前时间戳

    返回的时当前Date对象的时间戳

    console.log(Date.now()); //返回当前时间戳
    //1656648040077
    
    • 1
    • 2

    可以利用时间戳来测试代码性能/运行速度。

    var start_time = Date.now();
    for(var i = 0;i < 50 ; i++){
        console.log(i);
    }
    var end_time = Date.now();
    var time = end_time - start_time;
    console.log('该for循环花费了'+time/1000 +'s')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行结果:
    在这里插入图片描述

    更多题库和知识点总结,面试技巧请看=>牛客网,快来和我一起刷题吧!

  • 相关阅读:
    TMI4054H锂电池充电管理IC
    hive:常见日期函数
    Jenkinsfile语法
    机器学习(二):聚类算法1——K-means算法
    【前端三栏布局总结】常见的前端三栏布局有哪些
    Metrics-Server总结
    在直播系统中使用RTSP协议传递视频
    python 之f-strings 来格式化字符串
    y95.第六章 微服务、服务网格及Envoy实战 -- Front Proxy and TLS(六)
    word单元格自动换行,适应文字
  • 原文地址:https://blog.csdn.net/weixin_46277553/article/details/125554981