介绍
本文是在学习JS所做的学习笔记,所有笔记内容请看:JS学习笔记
Date
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
1、Date 对象
-在JS中使用Date对象来表示一个时间
创建一个Date对象
如果直接使用构造函数创建一个Date对像,则会封装为当前代码执行的时间
日期的格式 月份/日/年 时:分:秒
var d=new Date();
console.log(d)
创建一个指定时间
在构造函数中传递一个表示时间的字符串作为参数
var d2=new Date("12/03/2016 11:10:30");
console.log(d2);
2、getDate()
-获取当前日期对象是几日
var date=d2.getDate()
3、getDay()
-获取当前日期对象是周几
-会返回一个0~6的值
0表示周日
1表示周一
var day=d2.getDay();
console.log(day)
4、getMonth()
- 获取当前时间对象的月份
会返回一个0~11的值
0表示 1月
1表示2月
var month=d2.getMonth();
5、getTime()
-获取当前日期对象的时间戳
- 时间戳,指的是从格林威治标注时间的1970年1月1日,0是0分0秒
到当前日期所花费的毫秒数(1 秒=1000毫秒)
-计算机底层保存时间使用的都是时间戳
var time =d2.getTime();
console.log(time)
var d3=new Date("1/1/1970 0:0:0");//中国时间
6、获取当前时间戳
var time =Date.now();// 获取当前时间戳
console.log(time);
</script>
</head>
<body>
</body>
Math对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
1、Math
-Math和其它对象不同,它不是一个构造函数
它属于一个工具类,不用创建对象,他里边封装了数学运算
相关的属性和方法
- 比如
Math.PI表示一个常量
2、abs()计算一个数的绝对值
console.log(Math.abs(-1))
3、Math.ceil()
-可以对一个数进行向上取整,小数位只要有值就自动进1
Math.floor()
-可以对一个数进行向下取整,小数部分会被舍掉
Math.round()
-可以对一个数,四舍五入取整
console.log(Math.ceil(1.1))// 输出2
console.log(Math.floor(1.99)); //向下取整1
4、Math.random()
-生成一个0到1的随机数
- 生成0到x的随机数,则用Math.random()*x
-生成x-y之间的随机数
Math.round(Math.random()*(y-x))+x
console.log(Math.random());
console.log(Math.random()*10);//生成0~10的随机数
console.log(Math.round(Math.random()*10));// 生成0到10的整数
console.log(Math.round(Math.random()*9)+1);// 生成1到10的整数
5、max()
-可以获取多个数中最大值
min()
-可以获取多个数中的最小值
var max=Math.max(10,24,45);
var min=Maht.min(10,25,36)
6、Math.pow(x,y)
返回x的y次幂
Math.sqrt()
开方
</script>
</head>
<body>
</body>