0.前言
看到网上那些大神写的时间、钟表各种酷炫,心里痒痒的,因此今天就来说一些关于时间对象的知识,希望自己也能写出酷炫的钟表时钟,O(∩_∩)O哈哈~
1.了解时间
首先来了解两个时间:
(1)格林尼时间(GMT):是英国郊区格林尼治天文台的时间,因为地球自转的问题,每个时区的时间是不一样的。格林尼治天文台所处的是经度为0的地方,世界上一些重大的时间都是使用的格林尼治时间。
(2)世界协调时间(UTC):也叫世界时间。就是1970年1月1日0时。
2.Date
我们知道在不管是在移动端、还是PC端,都会用到时间对象Date,那么在JS中他是怎么来的呢?
JS中的Date类型是由早期Java中的java.util.Date类型基础上构建的。所以保存的是距离1970年1月1号0时的毫秒数来存储时间的。
(1)Date的创建
第一种:用Date()函数创建
var date1 = Date();
console.log(date1);
console.log(typeof date1);
运行结果:
上面获取的是系统当前的时间,通过打印看到这种创建方法的时间得到是一个字符串“string”。
第二种:用构造函数创建
这种方法又分四种类型:
1.不使用参数,得到当前时间
var date2 = new Date();
console.log(date2);
console.log(typeof date2);
结果:
和第一种方法的结果类似,都是得到当前时间,但是返回的数据类型是“object”。
2.使用参数,得到当前时间
注意:参数是一个表示时间的字符串
var date3 = new Date("2008/08/08 12:34:56");
console.log(date3);
//省略时分秒默认为0(默认是标准时间)
var date4 = new Date("2008/08/08");
console.log(date4);
var date5 = new Date("2008/9/18");
console.log(date5);
var date6 = new Date("2008-08-08 12:34:56");
console.log(date6);
var date7 = new Date("2008-09-03");//8点
console.log(date7);
var date8 = new Date("2008-1-1");//0点
console.log(date8);
结果:
从上面可以看到,当参数不同的时候,打印输出的职业是不同的,只用年月日,没有时间,打印输出的结果是时间默认为0,时间的格式不一样的话,打印出的时间点也不同。
3、参数是 年,月,日,时,分,秒,毫秒
var date9 = new Date(2015, 5, 12, 15, 67, 34, 654);
console.log(date9);
结果:
注意:年月必须写,月是从0开始,日是从1开始。
1、如果月份超过11,则年份自动增加
2、如果日期超过当月应该有的天数,月自动增加
3、时、分、秒、毫秒都如此
4、参数是一个数字
var date10 = new Date(2234);
console.log(date10);
结果:
返回值:是距离1970年1月1日0点参数毫秒之后的时间
注意:对应北京时间加8小时
3.Date对象的方法
这个比较简单,直接上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Date对象的方法</title>
</head>
<body>
<script type="text/javascript">
//得到当前时间
var date = new Date();
console.log(date);
//得到年:getFullYear()
console.log(date.getFullYear());
//得到月:getMonth()
console.log(date.getMonth());
//得到日:getDate()
console.log(date.getDate());
//得到星期:getDay()
console.log(date.getDay());
//得到时:getHours()
console.log(date.getHours());
//得到分:getMinutes()
console.log(date.getMinutes());
//得到秒:getSeconds()
console.log(date.getSeconds());
//毫秒:getMilliseconds()
console.log(date.getMilliseconds());
//getTime():当前对象表示的时间距离1970年1月1号0时的毫秒数
console.log(date.getTime());
//设置年:setFullYear()
date.setFullYear(1993);
//设置月:
//注意:月是从0开始的,如果月大于等于12,年份增加
date.setMonth(5);
//设置日期:setDate(),如果大于该月英因有的天数,月增加
date.setDate(17);
//星期一般不设置
//设置时:setHours(),如果大于23,日增加
date.setHours(7);
//设置分:setMinutes(),如果大于59,时增加
date.setMinutes(54);
//设置秒:setSeconds(),如果大于59,分增加
date.setSeconds(33);
//设置毫秒:setMilliseconds(),如果大于999,秒增加
date.setMilliseconds(666);
//设置距离1970年1月1号0时的毫秒数
date.setTime(1000);
console.log(date);
//转成字符串
//第一个:toLocaleString():包含年月日时分秒
console.log(date.toLocaleString());
//第二个:toLocaleDateString();包含年月日
console.log(date.toLocaleDateString());
//第三个:toLocaleTimeString();包含时分秒
console.log(date.toLocaleTimeString());
//得到字符串表示的时间距离1970年1月1号0时的毫秒数
var date1 = Date();
console.log(Date.parse(date1));
</script>
</body>
</html>
运行结果:
4.Date对象间的运算
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Date对象间的运算</title>
</head>
<body>
<script type="text/javascript">
var date1 = new Date("2016-10-10 12:09:43");
var date2 = new Date("2016-10-10 12:09:43");
//两个时间对象相减,得到的是这两个对象之间相差的毫秒数
console.log(date1 - date2);
//将两个时间字符串拼接在一起
console.log(date1 + date2);
</script>
</body>
</html>
5.总结
以上上Date属性和一些常用的方法,但这只是第一步,还有很多的,只是为大家给大家的开胃小菜罢了。希望打赏!!!!
网友评论