日期,定时器

120 阅读2分钟

Math

Math类型时不可以创建对象的,所有的属性和方法都是通过类名调用
    console.log(Math.PI);
​
Math.floor(参数):向下取整
    console.log(Math.floor(1.1));
    console.log(Math.floor(1.9));
    console.log(Math.floor(-1.9));
​
Math.ceil(参数):向上取整
    console.log(Math.ceil(1.1));
    console.log(Math.ceil(1.9));
    console.log(Math.ceil(-1.9));
​
Math.round(参数):四舍五入取整
    console.log(Math.round(1.1));
    console.log(Math.round(1.5));
​
Math.sqrt(number) 开平方根
    console.log(Math.sqrt(9));
    console.log(Math.sqrt(8.5));
​
Math.pow(m,n)  返回m的n次方
    console.log(Math.pow(2, 5));
​
Math.min(1,-2,3,4) 返回N个数最小值
    console.log(Math.min(1, -2, 3, 4));
​
Math.max(1,-2,3,4) 返回N个数最小值
    console.log(Math.max(1, 2, 3, 4));
​
Math.abs(number) 返回绝对值
    console.log(Math.abs(-10));

random

Math.random():返回0~1的随机数,左闭右开
获取任意区间随机值
    function rand(min, max) {
        return Math.round(Math.random() * (max - min) + min);
    }

彩虹条

1.如何生成随机颜色?
var color = "#";
var str = "0123456789abcdef";
for (var i = 0; i < 6; i++) {
    color += str.charAt(rand(0, 15));
}
console.log(color);
​
2.如何将生成的颜色赋值给li?
var oLi = document.getElementById("test");
oLi.style.fontSize = "56px";
oLi.style.backgroundColor = color;
​
3.如何批量获取li?
document.getElementsByTagName("标签名"):返回该标签的所有元素,存放在数组中
var oLis = document.getElementsByTagName("li");
oLis[0].style.backgroundColor = color;
//-------------------------------------------------------
setInterval(function() {
    var oLis = document.getElementsByTagName("li");
    var color = "#";
    var str = "0123456789abcdef";
    for (var i = 0; i < oLis.length; i++) {
        for (var j = 0; j < 6; j++) {
            color += str.charAt(rand(0, 15));
        }
        oLis[i].style.backgroundColor = color;
        color = "#";
    }
}, 50);

日期对象

日期对象的创建
创建了一个程序运行这一刻的日期对象
var date = new Date();
console.log(date);
​
如何获取年月日时分秒星期几
getFullYear:获取年
    console.log(date.getFullYear());
getMonth 月 0~11
    console.log(date.getMonth());
getDate 日
    console.log(date.getDate());
getHours 时
    console.log(date.getHours());
getMinutes 分
    console.log(date.getMinutes());
getSeconds 秒
    console.log(date.getSeconds());
getDay 星期几 0~6
    console.log(date.getDay());
​
-----------------------------------
按照指定格式输出日期
xxxx年xx月xx日 xx:xx:xx 星期x
var date = new Date();
​
function myDate(date) {
    var _y = date.getFullYear();
    var _m = toTwo(date.getMonth() + 1);
    var _d = toTwo(date.getDate());
    var _h = date.getHours();
    var _mm = date.getMinutes();
    var _s = date.getSeconds();
    var _w = date.getDay();
    var week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
​
    var str = _y + "年" + _m + "月" + _d + "日 " + _h + ":" + _mm + ":" + _s + " ";
​
    str += week[_w];
​
    return str;
}
//toTwo():将月,日格式变成双位
function toTwo(v) {
    return v >= 10 ? v : "0" + v;
}
​
console.log(myDate(date));
    toLocaleString按照本地格式打印日期
console.log(date.toLocaleString());

设置指定日期对象

日期格式字符串:"YYYY-MM-DD,hh:mm:ss"
    var date = new Date("2022-05-06,14:22:36");
​
new Date(时间戳);
时间戳:距离197011日相差的毫秒数
    Date.parse(日期字符串): 返回时间戳
        var date = new Date(Date.parse("2022-05-06,14:22:36"));   
        console.log(date.toLocaleString());

修改日期对象的时间

var date = new Date();
    setDate()      //改变Date对象的日期
    setHours()   //改变小时数
    setMinutes()   //改变分钟数
    setMonth()   //改变月份,从0开始
    setSeconds()   //改变秒数
    setFullYear()   //改变年份
​
date.setDate(date.getDate() + 10);
date.setHours(date.getHours() + 5);
​
console.log(date.toLocaleString());

定时器

循环定时器
setInterval
    功能: 周期性的执行回调函数
    参数: setInterval(回调函数, 时间间隔)
    返回值: 关闭定时器的钥匙
​
        var count = 0;
        var fun = function() {
            console.log(++count);
        }
​
        setInterval(fun, 1000);
​
        var count = 0;
        var time = setInterval(function() { //匿名函数
            console.log(++count);
            if (count == 5) {
                //关闭循环定时器的方法,参数为定时器的钥匙
                clearInterval(time);
            }
        }, 1000);
​
延时定时器
     setTimeout(function() {
         console.log("Boom!!!");
     }, 3000);

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

日期差

var date1 = new Date();
var date2 = new Date("2000-10-08");
//天
console.log(Math.round((date1 - date2) / 1000 / 60 / 60 / 24));