内置对象。

什么是内置对象?

  • 内置对象就是指js语言自带的一些对象,这些对象供开发者使用,并提供了一些常用或最基本而必须使用的(属性和方法)

内置对象种类:

math、date、array、string等

1.Math

数字对象,可对数字进行相关操作;

取整操作:

// 向下取整
  console.log(Math.floor(3.5))
  // 向上取整
  console.log(Math.ceil(2.1))
  console.log(Math.round(2.6))
  console.log(Math.round(-2.6))
  // * 四舍五入:负数.5会向大值入 
  console.log(Math.round(-2.5)) //-2
  console.log(Math.round(-2.6)) //-3

排序操作

  // 排序操作
  console.log(Math.max(13, 2, 3))
  console.log(Math.min(13, 2, 3))

生产随机数

 // math.random() :  (返回一个随机的小数  0 =< x < 1)
  function getRandom (min, max) {
    // min-max之间的一个随机数,其包含最大和最小值
    let number = Math.floor(Math.random() * (max - min) + 1) + min
    console.log('1-10取随机数:' + number)
  }
  getRandom(1, 10)

2. Date

     概述:日期对象,对日期进行获取等操作。

                需要new实例后使用

    使用:

// 生成当前时间的日期对象
 var now = new Date()

// 获取 年 月 日    
  let year = now.getFullYear()
  // 月份从0开始计算,所以加1
  let month = now.getMonth() + 1
  let date = now.getDate()
  let day = now.getDay()

案例:

1.补零操作:返回当前的时分秒格式 ==> 08:08:08

        当时分秒为个位数时 进行补零

 let now = new Date()
  let h = now.getHours()
  let m = now.getMinutes()
  let s = now.getSeconds()

  h = h < 10 ? '0' + h : h
  m = m < 10 ? '0' + m : m
  s = s < 10 ? '0' + s : s
  console.log(h + ':' + m + ':' + s);

             

2.倒计时操作: 返回设定日期距离当前日期的时分秒。

   思路:用设定日期的时间戳 -  当前日期的时间戳 = 剩余毫秒数。

              剩余毫秒数进行转化 = 倒计时。

  function daojishi (cTime) {
    // 设定日期的时间戳
    let cTimes = +new Date(cTime)
    // 当前日期的时间戳
    let noTimes = +new Date()
    // 当前日期距离设定日期的毫秒数
    let allS = (cTimes - noTimes) / 1000
    // 天
    let tian = parseInt(allS / 60 / 60 / 24)
    tian = tian < 10 ? '0' + tian : tian
    // 时
    let shi = parseInt(allS / 60 / 60 % 24)
    shi < 10 ? '0' + shi : shi
    // 分
    let fen = parseInt(allS / 60 % 60)
    fen = fen < 10 ? '0' + fen : fen
    // 秒
    let miao = parseInt(allS % 60)
    miao = miao < 10 ? '0' + miao : miao
    console.log(tian, shi, fen, miao)
  }

  setInterval(() => {
    daojishi('2022-8-22 0:01:30')
  }, 1000);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值