Math和Date:

1. Math对象

round 四舍五入获取整数

   console.log(Math.round(-1.5)); // 输出:-1

floor 向下取整 地板

   console.log(Math.floor(1.8)); // 输出:1
   console.log(Math.floor(1.3)); // 输出:1
   console.log(Math.floor(-1.6)); // 输出:-2

ceil 向上取整 天花板

   console.log(Math.ceil(1.2)); // 输出:2
   console.log(Math.ceil(1.9)); // 输出:2
   console.log(Math.ceil(-1.9)); // 输出:-1

abs 取绝对值

   console.log(Math.abs(-19)); // 输出:19
   console.log(Math.abs(-90.01)); // 输出:90.01

PI 返回圆周率

   console.log(Math.PI); // 输出:3.141592653589793   

min 返回 x 和 y 中的最小值

   console.log(Math.min(20, 30)); // 输出:20

max 返回 x 和 y 中的最大值

 console.log(Math.max(20, 30)); // 输出:30

pow 返回 x 的 y 次幂

   console.log(Math.pow(2, 4)); // 输出:16

random 返回一个介于 0 和 1 之间的随机数(包括0,不包括1)

    console.log(Math.random()); // 输出:0到1之间的随机数

2. Date对象

Date 对象用于处理日期和时间。

2.1 创建Date对象

// 创建当前日期和时间的Date对象
var date = new Date()
console.log(date);  // 输出:Tue Sep 19 2017 16:25:28 GMT+0800 (中国标准时间)

// 根据日期和时间创建Date对象
var date = new Date("2017/05/20 13:14:00");
console.log(date);  // 输出:Sat May 20 2017 13:14:00 GMT+0800 (中国标准时间)

2.2 Date对象方法
获取时间的方法:

JS中的Math和Date
设置时间的方法:

JS中的Math和Date

相关文章: