Math对象与Date对象

Math对象介绍:

  1. 是js内置的对象,可以直接使用 ;
  2. 它拥有一些数学常数属性和数学函数方法 ;
  3. Math 不是一个函数对象。
  4. Math 用于 Number 类型

Math的属性:

console.log(Math.E);
//2.718281828459045

//Math.PI 圆周率
console.log(Math.PI);
//3.141592653589793

Math的方法:

1.Math.abs() : 绝对值

console.log(Math.abs(5));//5
console.log(Math.abs(-5));//5

2.Math.round():四舍五入

console.log(Math.round(8.88));//9
console.log(Math.round(8.49));//8

3.Math.max/Math.min:最大最小值

console.log(Math.max(230, 324, 213));//324
console.log(Math.min(230, 324, 213));//213

4.Math.ceil():向上取整

说明:向上取整 只要小数位存在,不管是多少,都向上进一; 如果是整数,则不变

console.log(Math.ceil(8.99)); //9
console.log(Math.ceil(8.19)); //9
console.log(Math.ceil(8.0000001));//9
console.log(Math.ceil(8.000000)); //0

5.Math.floor():向下取整

只保留整数部位

console.log(Math.floor(8.99)); //8
console.log(Math.floor(8.19)); //8
console.log(Math.floor(8.0000001));//8
console.log(Math.floor(8.000000)); //8

6.Math.pow(x,y):幂运算

x的 y的次方

console.log(Math.pow(2, 3)); //8
console.log(Math.pow(2, 10)); //1024

7.Math.random():平方根

console.log(Math.sqrt(36));//6
console.log(Math.sqrt(2));//1.4142135623730951

8.Math.random():随机数🔴

说明: 结果是一个随机数 [0, 1); 随机数的取值范围 是 0 到 1; 包含0, 不包含 1 ;

console.log(Math.random());

随机数练习:🟢

  1. [0,5] 之间的随机整数
let ran = Math.ceil(Math.random() * 5);
let ran = Math.floor(Math.random() * 6);
let ran = Math.round(Math.random() * 5);
console.log(ran);
  1. 求 [5 , 13] 之间的随机整数
let res = Math.round(Math.random() *  8 + 5) ;
let res = Math.floor(Math.random() *  9 + 5) ;
console.log(res);
  1. 函数封装,求任意区间的一个随机整数
function random(a, b){
   return  Math.floor(Math.random() * (b - a + 1) + a);
}

// let r = random(1, 4)
// let r = random(10, 19)
let r = random(-10, 0)
console.log(r);

Date对象

注意💡:

  • 获取当前时间必须实例化
  • Date()构造函数的参数

​ 如果括号里面有时间,就返回参数里面的时间,例如日期格式字符串为‘2019-5-1’,可以写成 new Date('2019-5-1')或者new Date('2019/5/1')

  1. 获取(实例化)日期对象

    实例化是指在面向对象的编程中,把用类创建对象的过程称为实例化

let d = new Date();
console.log(d); 
方法名 说明 代码
getFullYear() 获取当前年份 dObj.getFullYear()
getMonth() 获取当月(0-11) dObj.getMonth()
getDate() 获取当天日期 dObj.getDate()
getDay() 获取星期几(周日0-周六6) dObj.getDay()
getHours() 获取当前小时 dObj.getHours()
getMinutes() 获取当前分钟 dObj.getMinutes()
getSeconds() 获取当前秒钟 dObj.getSeconds()
getTime() 获取日期毫秒(从1970/1/1开始计算) dObj.getTime()
  1. 获取年份
let year = d.getFullYear();
console.log(year);
  1. 获取月份🟡

​ 获取月份 在d.getMonth()基础上 需要 + 1

​ 范围 0 - 11 ; 0 代表1月份, 11 代表是12月份

let month = d.getMonth()+1;
console.log(month);
  1. 获取天数 获取一月中的第几天
let day = d.getDate();
console.log(day);
  1. 获取周几🟡

    获取一周中的某一天 范围 0 - 6; 0 表示星期天

let week = d.getDay();
console.log(week);
  1. 小时 24小时制
console.log(d.getHours());
  1. 分钟
console.log(d.getMinutes());
console.log(d.getSeconds());
  1. 毫秒 即自1970年1月1日(UTC)起经过的毫秒数
console.log(d.getTime());

设置时间

let d = new Date();
console.log(d);

//设置时间
d.setFullYear(1999)
d.setFullYear(2000)
d.setMonth(7)
d.setDate(20)
d.setHours(16)
d.setMinutes(50)
d.setSeconds(30)
//  星期几是不能设置的  
console.log(d);

//通过 new Date(参数) 设置时间
//参数是一个毫秒数  1970.1.1 开始计算的
let d = new Date(2000); 
//第一个是年份,第二个数月份 
let d = new Date(2000, 5); 
//年 月 日 
let d = new Date(2000, 5, 10); 
//年月日时分秒
let d = new Date(2000, 5, 10, 17, 20, 50);
//可以使用日期字符串 
let d = new Date('2020-12-12'); 
let d = new Date('2020-12-12 12:12:12'); 
console.log(d);
posted @ 2022-04-12 17:04  秋弦  阅读(31)  评论(0编辑  收藏  举报