目录
一、Date的简介
Date 对象是 JavaScript 原生的时间库。它以1970年1月1日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 方法用来解析日期字符串,返回该时间距离时间零点(1970年1月1日 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方法实现转为具体的时间,按照需要转换为对应的格式即可