JavaScript之Date对象的简介

1 篇文章 0 订阅
1 篇文章 0 订阅

目录

一、Date的简介

二、Date的用法 

1.普通函数用法

2.构造函数的使用方法

3.日期的运算 

 三、时间戳

 获取当前时间戳方法

 Date.now()

 Date.parse()

时间戳的转换进制

四、Date对象_实例方法/get类

五、Date对象_实例方法/set类 

六、对应时间转为时间戳 

七、时间戳转为对应时间  


一、Date的简介

    Date 对象是 JavaScript 原生的时间库。它以197011日00:00:00 作为时间的零点,可以表示的时间范围是前后各1亿天(单位为毫 秒)

二、Date的用法 

1.普通函数用法

Date 对象可以作为普通函数直接调用,返回一个代表当前时间的字
符串
       //直接输出Date()
        console.log(Date());
       /*打印为当前时间:
        * Mon Oct 30 2023 17:11:23 GMT+0800 (中国标准时间)
        */

2.构造函数的使用方法

Date 还可以当作构造函数使用。对它使用 new 命令,会返回一个 Date 对象的实例。如果不加参数,实例代表的就是当前时间
var today = new Date();
作为构造函数时, Date 对象可以接受多种格式的参数,返回一个该参数对应的时间实例
        //参数为时间零点开始计算的毫秒数
        console.log(new Date(1635215676627));
        //Tue Oct 26 2021 10:34:36 GMT+0800 (中国标准时间)
        // 参数为日期字符串
        console.log(new Date('January 6, 2020'));
        //Mon Jan 06 2020 00:00:00 GMT+0800 (中国标准时间)
        //参数为多个整数
        //代表年、月、日、小时、分钟、秒、毫秒
        console.log(new Date(2023, 10, 30, 17, 22, 0, 0));
        //Thu Nov 30 2023 17:22:00 GMT+0800 (中国标准时间)
        
        //当参数为这些的也可以正常输出
        console.log(new Date('2022-2-15'));
        console.log(new Date('2022/2/15')); 
        console.log(new Date('02/15/2022'));
        console.log(new Date('2022-FEB-15'));
        console.log(new Date('FEB, 15, 2022'));
        console.log(new Date('FEB 15, 2022'));
        console.log(new Date('Feberuary, 15, 2022'));
        console.log(new Date('Feberuary 15, 2022'));
        console.log(new Date('15 Feb 2022'));
        console.log(new Date('15, Feberuary, 2022'));

3.日期的运算 

        var d1 = new Date(2023,10,30);
        var d2 = new Date(2023,10,29);
        console.log(d1-d2);
        //86400000 输出为毫秒

 三、时间戳

时间戳是指格林威治时间 1970 01 01 00 00 00 (北京时间 1970 01 01 08 00 00 ) 起至现在的总秒数。
格林威治和北京时间就是时区的不同 Unix 20 世纪 70 年代初出现的一个操作系统, Unix 认为 1970 1月1 0 点是时间纪元。 JavaScript 也就遵循了这一约束

 获取当前时间戳方法

 Date.now()

Date.now 方法返回当前时间距离时间零点( 1970 1 1 日 00:00:00 UTC )的毫秒数,相当于 Unix 时间戳乘以1000
       console.log( Date.now());
       //1698894143903

 Date.parse()

Date.parse 方法用来解析日期字符串,返回该时间距离时间零点(197011 00:00:00)的毫秒数

       console.log( Date.parse('Aug 9, 2022')); //1659974400000
       console.log(Date.parse('January 26, 2022 13:51:50'));  //1643176310000
       console.log(Date.parse('Mon, 25 Dec 2022 13:30:00 GMT')); //1671975000000
       console.log(Date.parse('Mon, 25 Dec 2022 13:30:00 +0430')); //1671958800000
       console.log(Date.parse('2022-10-10')); //1665360000000
       console.log(Date.parse('2022-10-10T14:48:00'));  //1665384480000

 如果解析失败,返回 NaN

时间戳的转换进制

时间戳为ms级

①1s=1000ms 

②1分=60s

③1时=60分

④1天=24时

⑤1月=30天

⑥1年=365天

tips:小单位转大单位,乘以进制

四、Date对象_实例方法/get

实例方法 get
getTime() :返回实例距离 1970 1 1 00:00:00的毫秒数
getDate() :返回实例对象对应每个月的几号(从 1开始)
getDay() :返回星期几,星期日为 0 ,星期一为 1,以此类推
getYear() :返回距离 1900的年数
getFullYear() :返回四位的年份
getMonth() :返回月份( 0 表示 1 月, 11 表示 12月)
getHours() :返回小时( 0-23)
getMilliseconds() :返回毫秒( 0-999)
getMinutes() :返回分钟( 0-59)
getSeconds() :返回秒( 0-59)

五、Date对象_实例方法/set 

setDate(date) :设置实例对象对应的每个月的几号( 1-31
setYear(year) : 设置距离 1900 年的年
setFullYear(year [, month, date]) :设置四位年份
setHours(hour [, min, sec, ms]) :设置小时( 0-23
setMilliseconds() :设置毫秒( 0-999
setMinutes(min [, sec, ms]) :设置分钟( 0-59
setMonth(month [, date]) :设置月份( 0-11
setSeconds(sec [, ms]) :设置秒( 0-59
setTime(milliseconds) :设置毫秒时间戳

六、对应时间转为时间戳 

var date = new Date('2020-01-01 18:55:49:123');
var time1 = date.getTime();
console.log(time1);

 使用new Date创建一个时间对象,使用getTime()方法将其转换为时间戳格式,也可以使用Date.parse()方法进行直接转化,返回三大点查看使用方法

七、时间戳转为对应时间  

    var nowDate = new Date(Date.now());
    console.log(nowDate);
    console.log(nowDate.getFullYear());
    console.log(nowDate.getMonth());
    console.log(nowDate.getDate());
    console.log(nowDate.getHours());

使用Date对象的get方法实现转为具体的时间,按照需要转换为对应的格式即可 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农小段

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值