JavaScript Date
Date 用于创建一个 JavaScript Date 实例,该实例呈现时间中的某个时刻。
Date 对象使用的频率非常高,大量的业务需要对时间进行操作。
1. 基本使用
1.1 获取时间戳
当实例化时没有传递参数给 Date
的时候,则表示创建的对象为实例化时刻的时间。
var date = new Date();
var timestamp = date.getTime();
console.log(timestamp);
var date = new Date();
var timestamp = +date;
console.log(timestamp);
var date = new Date();
var timestamp = date.valueOf();
console.log(timestamp);
推荐使用 getTime
方法来获取时间戳,以便他人阅读代码以及避免不必要的问题。
1.2 格式化时间
格式化时间可以理解成把时间处理成想要的格式,如年-月-日 时:分;秒
。
通过 Date 对象提供的一些方法,可以获得到对应的时间属性。
假如想把时间格式化成年/月/日 时:分:秒
的形式:
var date = new Date();
var YYYY = date.getFullYear();
var MM = date.getMonth() + 1;
var DD = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
console.log([YYYY, '/', MM, '/', DD, ' ', hh, ':', mm, ':', ss].join(''));
通过 Date 对象提供的获取年、月、日、时、分、秒的方法获取到对应的值,最后按照想要的格式拼接即可。
需要注意的是 getMonth()
方法返回的月份是 0 至 11 ,更像是月份的索引,实际上对应的月份还要加上 1 。
2. 构造函数的参数
Date 对象可以提供 4 种类型的参数,通过参数决定时间,最后对象的实例的操作都围绕这个决定的时间。
2.1 不传递参数
当不传递参数的时候,时间会被设置为实例化那一时刻的时间。
2.2 Unix 时间戳
应用场景大部分为从服务端获取数据后,对时间戳进行格式化显示。
var data = { _id: '', createdAt: 1482632382582, content: '' };
var date = new Date(data.createdAt);
var YYYY = date.getFullYear();
var MM = date.getMonth() + 1;
var DD = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
console.log([YYYY, '/', MM, '/', DD, ' ', hh, ':', mm, ':', ss].join(''));
2.3 时间戳字符串
这里并不是指字符串形式的 Unix 时间戳
,而是符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601 标准的时间字符串。
实际上只要能被 Date.parse
正确解析成时间戳的字符串,都可以作为参数传递过去。
var timestamp = Date.parse('2020/02/02 11:22:33');
var date1 = new Date(timestamp);
var date2 = new Date('2020/02/02 11:22:33');
2.4 日期的每一个时间属性
这里的时间属性是指:年、月、日、时、分、秒、毫秒。
var date = new Date(2048, 10 - 1, 24, 9, 9, 6, 0);
var YYYY = date.getFullYear();
var MM = date.getMonth() + 1;
var DD = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
console.log([YYYY, '/', MM, '/', DD, ' ', hh, ':', mm, ':', ss].join(''));
第二个参数之所以要减去 1 ,是因为月份是从 0 开始计算的,所以十月应该表示成 9 。
3. 其他常用方法
4. 小结
通常会采用不传参
或者传递一个 Unix 时间戳
来生成 Date
实例,另几种参数形式使用场景较少。
需要注意的是,getMonth
方法返回的月份,是从 0
开始计数的,对应真实月份需要加上 1
。