javaScript的内置对象:系统提供的对象
- Object对象 是所有JavaScript对象的超类(基类)
- Array对象 数组对象–定义数组属性和方法
- Boolean对象 布尔对象–布尔值相关
- Date对象 日期对象–日期时间相关
- Error对象 错误对象–处理程序错误
- Function对象 函数对象–定义函数属性和方法
- Math对象 数学对象–各种数学运算工具(不是构造函数)
- Number对象 数字对象–定义数字属性和方法
- RegExp对象 正则表达式对象–定义文本匹配与筛选规则
10.String对象 字符串对象–定义字符串属性和方法
Date内置对象
- 日期对象创建
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数(时间戳)
- Date对象里的月份是 0~11,但是日期又是正常1开始
var date = new Date() // 得到的是当前时间对象
console.log(date)
console.log(typeof date) // object
console.log(Date.now()) // 获取的是当前的时间戳
// 获取任意时间的时间对象
// Date对象里的月份是 0~11,但是日期又是正常1开始
var date1 = new Date(2008, 2, 1, 8, 2, 3) //2008年2月1号8时2分3秒
get系列API
getFullYear() | 返回年 |
---|---|
getMonth() | 返回月份0–11 |
getDate() | 返回某一天 |
getDay() | 返回星期0-6 |
getHours() | 返回小时 |
getMinutes() | 返回分钟 |
getSeconds() | 返回秒 |
getTime() | 返回1970年1月1日午夜到指定日期(字符串)的毫秒数 |
set系列API
setFullYear() | 设置年份 |
---|---|
setMonth() | 设置月 |
setDate() | 设置天 |
setHours() | 设置小时 |
setMinutes() | 设置分钟 |
setSeconds() | 设置秒 |
setTime() | 使用毫秒的形式设置时间对象 |
★注意:
1、setDay( 这个真没有!!!,星期是通过设定日期自动计算的 )
2、set系列API可以设置比当前范围更精细的时间
比如:setFullYear(2012,3,5) 设置日期为2012年4月5号
setHours(13,30,0) 设置时间为13:30:00
实例: setFullYear(year,month,day)
year:
必需,表示年份的四位整数
month:
可选,介于 0 ~ 11 之间:如果不填,取系统当月
-1 为去年的最后一个月
12 为明年的第一个月
13 为明年的第二个月
date:
-
可选,表示月中某一天的数值。如果不填,取系统当日
-
用本地时间表示。介于 1 ~ 31 之间:
-
0 为上个月最后一天
-
-1 为上个月最后一天之前的天数
-
如果当月有31天:32 为下个月的第一天
-
如果当月有30天:
-
32 为下一个月的第二天
实例:现在时间:13:11:43
var date = new Date(2012, 2, 15, 13, 11, 43);
2012年3月15日 13:11:43
date.setFullYear(2013, 5);
2013年6月15日 13:11:43
date.setFullYear(2012, 20, 5);
2013年9月5日 13:11:43
date-API
var date = new Date()
console.log(date.getFullYear())
console.log(date.getYear()) // 现在已经不用了,获取的是1900到当前年份的差值
console.log(date.getMonth()) // 月份是 0~11
console.log(date.getDate()) // 得到日期的号数
console.log(date.getHours())
console.log(date.getMinutes())
console.log(date.getSeconds())
console.log(date.getDay()) // 获取星期几 0~6,星期天就是0
console.log(date.getMilliseconds()) // 获取当前日期的毫秒(一般用不到)1秒===1000毫秒
// 还有一系列设置API,可以把日期拨到某个时间点
date.setFullYear(2021) // 把date日期对象设置为2021年
date.setMonth(5) // 把日期拨到6月
date.setDate(20) // 把日期设置成20号
// date.setDay() // 这个方法是不存在的,星期几是根据日期自动计算的,不能手动设置
date.setHours(7)
date.setMinutes(7)
date.setSeconds(7)
// date.setTime(0) // 设置时间戳,date就会根据时间戳来计算,设置为0那么date九变成了1970-1-1,这个方法一般用的不多
set-API的特点
var date = new Date()
// set系列的API如果参数超过正常返回,日期会自动取往前或者往后推算
date.setMonth(12)
date.setDate(0) // 会把时间设置为上个月的最后一天
// set系列API可以设置更精细的范围
var date1 = new Date()
// date1.setFullYear(2008, 2, 1) // 日期拨到2008年3月1号,但是不能设置时分秒
date1.setMonth(2, 1) // 可以同时设置月和日
date1.setHours(3, 3, 3) //设置时分秒