注意:
该文章由JS小白(本人)编写完成,仅为个人总结和理解,若有纰漏和误解,还望多多指出,共同成长😋
Date对象
Date 对象用于处理日期与时间。可以使用下面的方法创建 Date 对象
var oDate = new Date()
Date 对象方法
| 方法 | 描述 |
|---|---|
| Date() | 返回当日的日期和时间。 |
| getDate() | 从 Date 对象返回一个月中的某一天 (1 ~ 31)。 |
| getDay() | 从 Date 对象返回一周中的某一天 (0 ~ 6)。 |
| getMonth() | 从 Date 对象返回月份 (0 ~ 11)。 |
| getFullYear() | 从 Date 对象以四位数字返回年份。 |
| getYear() | 请使用 getFullYear() 方法代替。 |
| getHours() | 返回 Date 对象的小时 (0 ~ 23)。 |
| getMinutes() | 返回 Date 对象的分钟 (0 ~ 59)。 |
| getSeconds() | 返回 Date 对象的秒数 (0 ~ 59)。 |
| getMilliseconds() | 返回 Date 对象的毫秒(0 ~ 999)。 |
| getTime() | 返回 1970 年 1 月 1 日至今的毫秒数。 |
| getTimezoneOffset() | 返回本地时间与格林威治标准时间 (GMT) 的分钟差。 |
| getUTCDate() | 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。 |
| getUTCDay() | 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。 |
| getUTCMonth() | 根据世界时从 Date 对象返回月份 (0 ~ 11)。 |
| getUTCFullYear() | 根据世界时从 Date 对象返回四位数的年份。 |
| getUTCHours() | 根据世界时返回 Date 对象的小时 (0 ~ 23)。 |
| getUTCMinutes() | 根据世界时返回 Date 对象的分钟 (0 ~ 59)。 |
| getUTCSeconds() | 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。 |
| getUTCMilliseconds() | 根据世界时返回 Date 对象的毫秒(0 ~ 999)。 |
| parse() | 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。 |
| setDate() | 设置 Date 对象中月的某一天 (1 ~ 31)。 |
| setMonth() | 设置 Date 对象中月份 (0 ~ 11)。 |
| setFullYear() | 设置 Date 对象中的年份(四位数字)。 |
| setYear() | 请使用 setFullYear() 方法代替。 |
| setHours() | 设置 Date 对象中的小时 (0 ~ 23)。 |
| setMinutes() | 设置 Date 对象中的分钟 (0 ~ 59)。 |
| setSeconds() | 设置 Date 对象中的秒钟 (0 ~ 59)。 |
| setMilliseconds() | 设置 Date 对象中的毫秒 (0 ~ 999)。 |
| setTime() | 以毫秒设置 Date 对象。 |
| setUTCDate() | 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。 |
| setUTCMonth() | 根据世界时设置 Date 对象中的月份 (0 ~ 11)。 |
| setUTCFullYear() | 根据世界时设置 Date 对象中的年份(四位数字)。 |
| setUTCHours() | 根据世界时设置 Date 对象中的小时 (0 ~ 23)。 |
| setUTCMinutes() | 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。 |
| setUTCSeconds() | 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。 |
| setUTCMilliseconds() | 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。 |
| toSource() | 返回该对象的源代码。 |
| toString() | 把 Date 对象转换为字符串。 |
| toTimeString() | 把 Date 对象的时间部分转换为字符串。 |
| toDateString() | 把 Date 对象的日期部分转换为字符串。 |
| toGMTString() | 请使用 toUTCString() 方法代替。 |
| toUTCString() | 根据世界时,把 Date 对象转换为字符串。 |
| toLocaleString() | 根据本地时间格式,把 Date 对象转换为字符串。 |
| toLocaleTimeString() | 根据本地时间格式,把 Date 对象的时间部分转换为字符串。 |
| toLocaleDateString() | 根据本地时间格式,把 Date 对象的日期部分转换为字符串。 |
| UTC() | 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。 |
| valueOf() | 返回 Date 对象的原始值。 |
常用Date对象方法
1.获取年份 getFullYear()
var oDate = new Date();
var year = oDate.getFullYear();
2.获取月份 getMonth()
var oDate = new Date();
var mon = oDate.getMonth();
//返回值是 0(一月) 到 11(十二月) 之间的一个整数
3.获取日期 getDate()
var oDate = new Date();
var date = oDate.getDate();
4.获取星期几 getDay()
var oDate = new Date();
var day = oDate.getDay();
//返回值是 0(周日) 到 6(周六) 之间的一个整数
5.获取小时 getHours()
var oDate = new Date();
var hour = oDate.getHours();
6.获取分钟 getMinutes()
var oDate = new Date();
var min = oDate.getMinutes();
7.获取秒 getSeconds()
var oDate = new Date();
var sec = oDate.getSeconds();
以上为获取当前时间的方法,如果将get变为set,并在括号中填入参数,就为设置时间的方法
例如:
var oDate = new Date()
oDate.setFullYear(2000)
console.log(oDate)
会得到以下这样的结果
Thu Mar 16 2000 21:01:15 GMT+0800 (中国标准时间)
当然,你也可以直接在new Date()中直接添加参数,也可获得以上效果,但是请注意,这个参数是有格式要求的:
new Date();
// Mon Mar 16 2020 21:12:54 GMT+0800 (中国标准时间)
new Date(1584364026973);
// Mon Mar 16 2020 21:07:06 GMT+0800 (中国标准时间)
new Date('2020-01-01T12:00:00')
// Wed Jan 01 2020 12:00:00 GMT+0800 (中国标准时间)
new Date('2020/01/01 12:00:00')
// Wed Jan 01 2020 12:00:00 GMT+0800 (中国标准时间)
new Date('Jan 01 2020 11:00:00')
// Wed Jan 01 2020 11:00:00 GMT+0800 (中国标准时间)
new Date('asdfasdfas');
new Date('2020-01-01-12:12:12')
new Date('1292343600000');
//Invalid Date
new Date('2020-01-01 11:00:00')-new Date('2000/01/01 12:00:12')
// 631148388000
1.无参数的时候,返回当前时区当前时间的字符串形式
2.参数为一串数字的时候,会被当做毫秒数计算,时间从1970.01.01开始计算
3.对于格式不正确的参数,哪怕是字符串类型的毫秒数,会返回Invalid Date
4.虽然new Date()的返回值是字符串,但是其结果是可以直接相减的,结果为相差的毫秒数
以上是关于JS第十三小节的总结,下次再见😘