JavaScript 内置对象

目录

一. math (数学对象)

1. math常用方法 abs() 绝对值

2. math常用方法 ceil() 向上取整

3. math常用方法------最值

4. math常用方法 pow() 一个数的多少次幂

5. math常用方法  random()   随机数(0,1)

6. Math.round(x)  四舍五入

7. 小案例

8.  猜数字游戏

二. Date(日期对象)

1. 基础使用

2. 日期方法

3. 案例:格式化时分秒

4. 案例:倒计时

三. 日期对象特殊使用(计算函数的运行时间)


一. math (数学对象)

1. math常用方法 abs() 绝对值

//math常用方法 abs()  绝对值
Math.abs('-1');     // 1
Math.abs(-2);       // 2
Math.abs(null);     // 0
Math.abs("string"); // NaN
Math.abs();         // NaN

console.log(Math.abs('-1'));

2. math常用方法 ceil() 向上取整

//math常用方法 ceil()  向上取整
console.log(Math.ceil(.95));
// expected output: 1

console.log(Math.ceil(4));
// expected output: 4

console.log(Math.ceil(7.004));
// expected output: 8

console.log(Math.ceil(-7.004));
// expected output: -7

3. math常用方法------最值

// math常用方法  max() 最大值
console.log(Math.max(1, 3, 2));
// expected output: 3

console.log(Math.max(-1, -3, -2));
// expected output: -1


// math常用方法  min()  最小值
console.log(Math.min(10,20));  //10

4. math常用方法 pow() 一个数的多少次幂

// math常用方法 pow() 一个数的多少次幂 
console.log(Math.pow(2,3))  // 8

5. math常用方法  random()   随机数(0,1)

// math常用方法  random()   随机数(0,1)
console.log(Math.random())

6. Math.round(x)  四舍五入

//Math.round(x)  四舍五入
console.log(Math.round(0.4));  //0
console.log(Math.round(6));  //6
console.log(Math.round(1.4)); //1
console.log(Math.round(1.6));  //2

7. 小案例

//得到一个俩数之间的随机数 
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}


//随机整数
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}


//随机整数优化
function getRandomInt(min, max) {
    return Math.round(Math.random() * (max - min) + min);
}

let result = getRandomArbitrary(1,3)
console.log(result);

8.  猜数字游戏

function getRandomInt(min, max) {
    return  Math.round(Math.random() * (max - min) + min);
}

  let result = getRandomInt(1,10);

  console.log(result);

  while(true){
      let  num = prompt("请输入你猜的值!");
      if(num>result){
          alert("过了啊");
      }else if(num<result){
          alert("差远了");
      }else{
          alert("哎!对喽");
          break;
      }
  }

二. Date(日期对象)

1. 基础使用

//基础使用
let now = new Date();


//传值
let d1 = new Date(2000,0);
console.log(d1);  //本地时间2000年 1月1日零点

let d2  = new Date(2005,4,5,17,55,55);  //本地时间2005年5月5日下午5点55分55秒
let d3 = new Date("2021/9/16 11:03:55")
console.log(d2);


//日期格式化时间 toLocaleString
let data1 = new Date();
let result = data1.toLocaleString();  //返回与浏览器运行的本地环境一致的日期和时间
console.log(result);

2. 日期方法

let time = new Date();
    
console.log(time.getFullYear());  //获取年份

console.log(time.getMonth()); //获取月份  0~11   0表示一月

console.log(time.getDate());  //获取天数 
   
console.log(time.getHours());   //获取当前的小时

console.log(time.getMinutes());  //获取分钟

console.log(time.getSeconds());  //获取秒

console.log(time.getTime());  //获取毫秒数

3. 案例:格式化时分秒

  function formatTime(){
       let time = new Date();
       let hour = time.getHours();
       let minute = time.getMinutes();
       let second = time.getSeconds();

       //格式化时间
       hour = hour<10?"0"+hour:hour;
       minute = minute<10?"0"+minute:minute;
       second = second<10?"0"+second:second;

       return "北京时间:"+hour+":"+minute+":"+second;
   }

  let result = formatTime();

  console.log(result);

4. 案例:倒计时

function reCount(time){
   let now = new Date();
   let future = new Date(time);
   let nowTime = now.getTime();
   let futurnTime = future.getTime();
   let limitTime = (futurnTime - nowTime)/1000;
   let day = parseInt(limitTime/60/60/24);
   let hour = parseInt(limitTime/60/60%24);
   let minutes = parseInt( limitTime/60%60);
   let seconds = parseInt( limitTime%60 );

   return "距离明年的现在还剩"+day+"天"+hour+"小时"+minutes+"分钟"+seconds+"秒";

}
let result = reCount("2023-4-13 00:00:00");
console.log(result);

三. 日期对象特殊使用(计算函数的运行时间)

let start  = Date.now();  //返回方法执行前的毫秒数

runTime();

let stop = Date.now();

result = stop - start;

console.log(result);

function runTime(){
    for(let i = 0;i<1000;i++){
        console.log("运行一下");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浪漫的宇航员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值