JavaScript 中的 Date 对象
// Chrome Console
> Date;
<· ƒ Date() { [native code] }
> Date();
<· "Tue Jul 23 2019 08:56:20 GMT+0800 (中国标准时间)"
内容
const date = new Date();
| key | value |
|---|---|
| date | Tue Jul 23 2019 09:01:11 GMT+0800 (中国标准时间) |
| date.constructor | ƒ Date() { [native code] } |
| date.getDate() | 23 |
| date.getDay() | 2 |
| date.getMonth() | 6 |
| date.getFullYear() | 2019 |
| date.getHours() | 9 |
| date.getMinutes() | 1 |
| date.getSeconds() | 11 |
| date.setDate(12) | 1562893271338 |
| date.getDate() | 12 |
| date.setMonth(1) | 1549933271338 |
| date.getMonth() | 1 |
时间格式
3 种时间格式:
- GMT(Greenwich Mean Time),即格林尼治平均时;
- UTC(Universal Time Coordinated),即通用协调时;
- Timestamp,即时间戳,是指格林尼治时间1970年01月01日00时00分00秒起至现在的毫秒数。
GMT 与 UTC 一样,都是指英国伦敦的本地时间,区别在于 GMT 是根据地球的自传和公转来计算时间,UTC 是根据原子钟来计算时间。例如,GMT、UTC:Sun Jul 08 2018 15:30:26 GMT+0800,其中,GMT+0800 表示时区,+0800 表示比格林尼治时间时间多了 8 个时区,也就是北京时间比伦敦时间晚 8 个小时。
Date 内置对象
一、作普通函数调用
返回 GMT 格式的当前时间的字符串,传入任何参数都无效。
// Chrome Console
> Date();
<· "Tue Jul 23 2019 09:18:57 GMT+0800 (中国标准时间)"
> Date('2018-06-24');
<· "Tue Jul 23 2019 09:19:32 GMT+0800 (中国标准时间)"
二、作构造函数用
使用 new 操作符,则返回 GMT 格式的时间对象,其参数分为 3 类:
1、时间戳,number类型
// Chrome Console
> new Date(1549933271338);
<· Tue Feb 12 2019 09:01:11 GMT+0800 (中国标准时间)
2、表示日期的字符串
- yyyy-MM-dd HH:mm:ss
// Chrome Console > new Date(''2019-07-23 08:56:20''); <· Tue Jul 23 2019 08:56:20 GMT+0800 (中国标准时间) - yyyy-MM-ddTHH:mm:ss
其实跟第一个是一个意思。// Chrome Console > new Date(''2019-07-23T08:56:20''); <· Tue Jul 23 2019 08:56:20 GMT+0800 (中国标准时间) - Sun Jul 08, 2018 16:18:54 GMT
其实就是 GMT(UTC) 格式的字符串。// Chrome Console > new Date('Tue Jul 23 2019 09:01:11 GMT'); <· Tue Jul 23 2019 17:01:11 GMT+0800 (中国标准时间)
3、(year, month, day, hour, minutes, seconds, milliseconds)
// Chrome Console
> new Date(2019, 07, 23, 09, 28, 55)
<· Fri Aug 23 2019 09:28:55 GMT+0800 (中国标准时间)
4、如果不传参数,则会根据系统设置的当前时间来创建一个 Date 对象
三、需要注意的地方
- 构造函数的参数省略
关于 Date 构造函数的参数省略(是指省略其中的年、月、日之类的,不是省略整个参数)可能会出错。- 只指定“时分秒”,会出错
// Chrome Console > new Date('09:33:55'); <· Invalid Date - 分隔符非常重要,可以省略具体时间,但不能省略分隔符
// Chrome Console > new Date('-07- 09:33:55'); <· Sun Jul 01 2001 09:33:55 GMT+0800 (中国标准时间) > new Date('23 09:35:55'); <· Invalid Date
- 只指定“时分秒”,会出错
- JavaScript中,月份是从 0 开始计时的,比如用 2019-07-23 初始化的时间对象获取月份
// Chrome Console > new Date('2019-07-23').getMonth(); <· 6 getDate()是返回一个月的第几天,getDay()是返回一星期的第几天,其中 0 为星期天- 获取年份是
getFullYear(),而不是getYear() valueOf()返回原始值,与调用getTime()一样,返回时间戳,一般是用于内部调用,可能是getTime()的内部实现
四、工具函数
// javascript
/**
* 时间戳 转换为 yyyy-MM-dd HH:mm:ss
* @param {number} timestamp
* @param {string} format
*/
function timestampToDateTime(timestamp = Date.now(), format = 'yyyy-MM-dd HH:mm:ss') {
if (isNaN(timestamp)) {
return '';
}
if (format.length < 4 || 'yyyy-MM-dd HH:mm:ss'.indexOf(format) !== 0) {
return '';
}
const date = new Date(Number(timestamp));
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return format.replace('yyyy', year)
.replace('MM', month > 9 ? month : `0${month}`)
.replace('dd', day > 9 ? day : `0${day}`)
.replace('HH', hour > 9 ? hour : `0${hour}`)
.replace('mm', minute > 9 ? minute : `0${minute}`)
.replace('ss', second > 9 ? second : `0${second}`);
}