math、math.random、彩虹桥、date日期对象、datetostring、设置指定日期时间格式、设置日期、日期差、定时器、电子时钟、

这篇博客介绍了JavaScript中Math对象的常用方法,如取整、开方、指数运算等,并展示了如何生成随机数和日期对象。同时,讲解了日期对象的属性访问以及如何设置和计算日期差。此外,还讨论了定时器的使用,包括普通定时器和延时定时器的实现。最后,给出了创建电子时钟的例子。
摘要由CSDN通过智能技术生成

math

Math:特点,所有的方法都是直接通过类名来调用的

Math.floor(参数):向下取整 (负数取小的);

Math.ceil(参数):向上取整 (负数取大的);

Math.round(参数):四舍五入取整;

Math.sqrt(参数):开方;

Math.pow(m,n):返回m的n次方

Math.min(1,-2,3,4...):返回最小值

Math.max(1,-2,3,4...):返回最大值

Math.abs(参数):返回绝对值

math.random

math.random();

console.log(math.random());

生成任意区间随机值

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

彩虹桥

.如何生成随机颜色6
  颜色是由6位16进制的数据构成

、如果批量获取li标签
   document.getElementsByTagName(标签名):返回一个数组,数组存储的是标签名的所有HTML元素
     var oLis = document.getElementsByTagName("li");
     3.如何将生成的随机颜色赋值给li
     let oLi = document.getElementById("test");
    通过js的姿势访问HTML元素的属性
     oLi.style.backgroundColor = color;

日期对象

//获取年
    console.log(date.getFullYear());
    //月 0~11
    console.log(date.getMonth());
    //日 
    console.log(date.getDate());
    //星期几 0~6
    console.log(date.getDay());
    //时
    console.log(date.getHours());
    //分
    console.log(date.getMinutes());
    //秒
    console.log(date.getSeconds());

datetostring

console.log(dateToString(date));
    //toLocaleString():按照本地日期格式打印
    console.log(date.toLocaleString());

通过字符串设置指定日期

格式字符串 "yyyy-MM-dd[,hh:mm:ss]";

var date = new Date("1998-6-13,16:22:33");
     console.log(date.toLocaleString());

时间戳:某个日期距离1970/1月/1日零点相差的毫秒数

Date.parse(日期格式字符串):返回该字符串距离标准时间相差的毫秒数。

console.log(Date.parse("1998-6-13,16:22:33"));

设置日期

 

设置日期的方法
    把get改成set
    在设置时间时,常量的单位为日期操作数的单位 

date.setHours(date.getHours()+2);

日期差 

var date1 = new Date();
    var date2 = new Date("1940-8-25");
    console.log(Math.round((date1-date2)/1000/60/60/24)); 

定时器

关闭定时器的标记

var count = 0;
    clearInterval(定时器的钥匙)
     var time = setInterval(function(){
         console.log(++count);
         if(count == 5){
             clearInterval(time);
       }
     },1000); 

循环定时器
    setInterval(回调函数,时间间隔);返回值为关闭定时器的钥匙
     var count = 0;
     var fun = function(){
         console.log(++count);
     }
     setInterval(fun,2000);

延时定时器
    setTimeout(回调函数,时间间隔);
    setTimeout(function(){
        console.log("heihei");
    },3000);var oTime = document.getElementById("time");

 电子时钟
    

setInterval(function(){
        var date = new Date();
        oTime.value = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
    },1000); 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值