一. 日期
- 日期对象存储从1970-01-01开始到现在的毫秒数.
1.创建日期对象
- var date = new Date();
var date = new Date();
console.log(date); // Wed Jan 12 2022 11:51:12 GMT+0800 (中国标准时间)
2. 指定日期的格式
- var date = new Date("2021-01-12 11:18:00:00");
- var date = new Date("1970/01/12 8:0:00");
- var date = new Date(1970, 1, 12, 8, 0, 00);
3. 获取日期的方式
- 年, 以四位数返回年份
- date.getFullYear()
var date = new Date();
console.log(date.getFullYear()); // 2022
- 月, 取值0~11, 0代表1月
- date.getMonth()
var date = new Date();
console.log(date.getMonth() + 1); // 1
- 日, 取值1~31
- date.getDate()
var date = new Date();
console.log(date.getDate()); // 8
- 星期, 取值0~6, 0代表星期日
- date.getDay()
var date = new Date();
console.log(date.getDay()); // 3
- 时, 取值0~23
- date.getHours()
var date = new Date();
console.log(date.getHours()); // 9
- 分, 取值0~59
- date.getMinutes()
var date = new Date();
console.log(date.getMinutes()); // 44
- 秒, 取值0~59
- date.getSeconds()
var date = new Date();
console.log(date.getSeconds()); // 42
- 毫秒, 取值0~999
- date.getMilliseconds()
var date = new Date();
console.log(date.getMilliseconds()); // 588
4. 设置日期的方式
[注] 除了星期(Day)没有set外, 其它都有,因为星期是根据日期系统自动判断的
- setDate() 设置日期
- setMonth() 设置月份
- setFullYear() 设置年份
- setHours() 设置小时
- setMinutes() 设置分钟
- setSeconds() 设置秒数
- setMilliseSeconds() 设置毫秒数
- setTime(时间戳) 设置时间戳, 显示新的日期和时间
// eg.计算你出生那天星期几?
var date = new Date();
date.setFullYear(1999); // 设置年
date.setMonth(0); // 设置月 0-11
date.setDate(19); // 设置日 1-31
var list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
console.log(list[date.getDay()]); // 星期二
5. 获取时间戳的方式
- date.valueOf() 返回date的原始值(毫秒数)
- date.getTime() 返回1970年1月1日至今的毫秒数
- Date.now() 返回1970年1月1日至今的毫秒数
- Date.parse("2022-12-12 12:12:00:00") 返回指定日期的毫秒数
var date = new Date();
console.log(date.valueOf()); // 1644977964050
console.log(date.getTime()); // 1644977964050
console.log(Date.now()); // 1644977964050
console.log(Date.parse("2022-12-12 12:12:00:00")); // 1670818320000
6. 使用时间戳返回日期的计算方法
- 天: 时间戳/1000/60/60/24
- 时: 时间戳/1000/60/60%24
- 分: 时间戳/1000/60%60
- 秒: 时间戳/1000%60
- 毫秒: 时间戳%1000
二. 对象
- 对象的组成: 属性 + 方法
1. 对象的创建
- 构造函数 (new可以省略, 但是不建议)
var dog = new Object();
dog.name = '大黄';
dog['age'] = 2;
dog.run = function() {};
- 字面量表达式
var person = {
name: '小明',
age: 10,
run: function() {
console.log('跑步');
}
};
2. 添加属性和方法
- 添加属性
var person = {};
person.address = '中国';
person['sex'] = '男';
- 添加方法
var person = {
run: function() {
return '执行了run方法';
}
};
person.eat = function() {
return 'eat食物';
};
person['sleep'] = function() {};
3. 获取对象的属性和方法
1.获取属性
var person = {
address : '中国',
sex :'男'
};
console.log(person.address); // 中国
console.log(person['sex']); // 男
- 获取方法
var person = {
eat(){
return '执行了eat方法';
},
sleep: function() {
return '执行了sleep方法';
}
}
console.log(person.eat()); // 执行了eat方法
console.log(person['sleep']()); // 执行了sleep方法
4. 删除方法和属性
delete person.age
delete person['sleep']
三. 定时器
- 定时器包含定时器和延时器
- 定时器延时器编号从1开始
- 两种定时器使用的编号池是同一个编号池,在同一个编号池中已经被使用的编号不会再次使用
1. 定时器 setInterval()
- 格式: setInterval(code, millisecond)
- code: 要调用的代码块或函数
- millisecond: 每次执行的毫秒数
- 按照指定的周期(以毫秒)调用函数或表达式
- 关闭定时器: clearInterval(timer)
var timer = setInterval(function() {
console.log('哈哈');
}, 1000);
console.log(timer); // 1
clearInterval(timer); // 关闭定时器
2. 延时器 setTimeout()
- 格式: setTimeout(function(形参1, 新参2, ...){}, millisecond, 实参1, 实参2, ...)
- 指定的时间过后, 执行一次代码
- 关闭延时器: clearTimeout(timer)
var timer = setTimeout(function(a, b) {
console.log(a, b);
}, 1000, '哈哈', '嘿嘿');
console.log(timer); // 1
clearTimeout(timer); // 关闭延时器
3. 同步异步执行机制
- 同步优先(按顺序执行)
- 同步运行完之后异步进程按运行时间执行
console.log('11111111111');
setInterval(function() {
console.log('嘿嘿嘿嘿')
}, 2000);
setInterval(function() {
console.log('哈哈哈哈')
}, 1000);
console.log('22222222222');
// 结果: 11111111111 -> 22222222222 -> 哈哈哈哈 -> 嘿嘿嘿嘿