首先声明 MDN文档中也有详细介绍, 此处仅仅讲述常用的方法,以及牵涉的知识点
1.UTC是协调世界时(Universal Time Coordinated)的英文缩写. UTC相当于本初子午线(即经度0度)上的平均太阳时.其中需要注意的是, 北京时间比UTC时间早8个小时.
2. Date对象常用的是字符串类型
其中 Date对象是一个构造函数, 所以我们必须需要实例化之后才能使用
Date实例用来处理日期和时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// 1. 使用Date 如果没有参数 返回当前系统的当前时间
// var date = new Date();
// alert(date);
// 2. 参数常用的写法 数字型 2019, 10, 01 或是字符串型 '2019-10-1 8:8:8'
var date1 = new Date(2018, 10, 1);
alert(date1);
// var date2 = new Date('2018-10-1 8:8:8');
// alert(date2);
</script>
</head>
<body>
</body>
</html>
3.
方法名 | 解释 |
getFullYear() | 获取当年 |
getMonth() | 获取当月(0-11) |
getDate() | 返回是几号 |
getDay() | 获取星期几 (其中 周日为0 到周六 6) |
getHours() | 获取当前小时 |
getMinutes() | 获取当前分钟 |
getSeconds() | 获取当前秒钟 |
1). 获取当前时间必须实例化
var now = new Date();
console.log(now);
2) Date() 构造函数的参数
如果括号里面有时间, 就返回参数里面的时间. 例如日期格式字符串为 '2018-8-8', 可以写成 new Date('2018-8-8')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// 格式化日期 年月日
var date = new Date();//这个必须写 这个写完之后 后面的才可以使用
alert(date.getFullYear()); //getFullYear()当前日期的年
alert(date.getMonth() + 1); //getMonth()返回的月份比当前月份小1一个月 所以要+1
alert(date.getDate()); //返回是几号
alert(date.getDay()); // 周一返回是1 周六返回是6 但是周日返回是0
var year = date.getFullYear();
var mouth = date.getMonth() + 1;
var dates = date.getDate();
var arr = ['星期日', '星期一','星期二', '星期三', '星期四', '星期五', '星期六'];
var day = arr[date.getDay()];
alert('今天是' + year + '年' + mouth + '月' + dates + '日' + day);
</script>
</head>
<body>
</body>
</html>
4. Date 对象是基于 1970年1月1日(世界标准时间) 起的毫秒数
获得Date总的毫秒数(也叫做时间戳)
我们经常利用总的毫秒数来计算时间, 因为它更精确
我们Date总的毫秒数, 不是当前时间的毫秒数, 而是距离 1970年1月1号过了多少毫秒数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var date = new Date(); //这个不用多说 用来Date内置对象就一定要写
// 1.通过 valueOf() getTime()
console.log(date.valueOf()); //输出的是 我们目前的时间距离 1970.1.1总的毫秒数
console.log(date.getTime());
// 2.简单的写法(最常用的写法)
var date1 = +new Date(); // +new Date() 返回的就是总的毫秒数
console.log(date1);
// 3. H5新增的 获得总的毫秒数
console.log(Date.now());
</script>
</head>
<body>
</body>
</html>
5. 倒计时Demo // 没有完全实现 仅仅写出来其中的核心代码 其余部分请自行补充哦
插播一条知识点 : 1秒 = 1000毫秒
核心算法: 就是输入的时间减去现在的时间就是剩余的时间, 也就是倒计时. 但是要注意不能用时分秒直接相减, 会出现负数, 比如05分减去25分
实现思路:
第一步:用时间戳来实现倒计时. 用户输入时间总的毫秒数减去现在时间的总的毫秒数, 得到的就是剩余时间的毫秒数
第二步:把剩余时间总的毫秒数转换为 天, 时, 分, 秒(时间戳转换为时分秒)
转换公式如下:
d = parselnt(总秒数/60/60/24) //计算天数
h = parselnt(总秒数/60/60%24) //计算小时
m = parselnt(总秒数/60%60) //计算分数
s = parselnt(总秒数%60) //计算当前的秒数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function conutDown(time) {
var nowTime = +new Date(); //返回的是当前时间总的毫秒数
var inputTime = +new Date(); //返回的是用户输入时间总的毫秒数
var times = (inputTime - nowTime) / 1000; //time是剩余时间总的毫秒数
}
</script>
</head>
<body>
</body>
</html>