数学操作和时间日期

273 阅读4分钟

数学操作

随机数 - random()

console.log( Math.random() ); // 获取到的结果是 0~1之间的随机小数,可能会得到0,但是永远得不到1
console.log( parseInt(Math.random() * 10) + 1 ); // 最小1,最大10

a~b之间的随机整数
function getRandom(a, b) {
    // 判断大小
    var max = a
    var min = b
    if (a < b) {
        max = b
        min = a
    }
    var num = parseInt(Math.random() * (max - min)) + min
    return num
}
// var n = getRandom(20, 100)
var n = getRandom(10, 5)
console.log(n);

向上取整 - Math.ceil()

console.log( Math.ceil(3.14) );

向下取整 - Math.floor()

console.log( Math.floor(4.99999) );

四舍五入 - Math.round()

console.log( Math.round(3.14) );

求最大值 - Math.max(多个数字)

console.log( Math.max(1,5,9,7,6,4,3,8,2) );

最小值 - Math.min(多个数字)

console.log( Math.min(1,5,9,7,6,4,3,8,2) );

求次方 - Math.pow(底数, 幂)

console.log( Math.pow(2, 3) );

开平方根 - Math.sqrt(数字)

console.log( Math.sqrt(4) );

圆周率 - Math.PI

console.log( Math.PI );

正弦 - Math.sin(弧度)

1弧度 = 半径
360度的弧度 = 2π * 半径
30度的弧度 = 2π * 半径 / 360 * 角度
console.log( Math.sin(30  * 2 * Math.PI / 360) );

余弦 - Math.cos(弧度)

console.log( Math.cos(60 * Math.PI * 2 / 360) );

进制转换

将10进制转成其他进制

 数字.toString(目标进制数)

var a = 3
console.log( a.toString(2) );

将其他进制转成10进制

parseInt(要转的数字, 将他看做多少进制)

var a = '11'
console.log( parseInt(a, 2) );
console.log( parseInt(a, 8) );

获取随机的颜色值,并通过点击浏览器窗口改变body的背景颜色
var color = '#'
for (var a = 0; a < 3; a++) {
    var num = getRandom(0, 256)
    num = num.toString(16)
    // 通过判断,一定要保证每次得到的随机16进制 是两位数
    if (num.length < 2) {
        num = '0' + num
    }
    color += num
}
console.log(color);
// 代表body标签 --- >    window.document.body
// 设置标签的样式 --->    标签.style.css键 = 值
// window.document.body.style.backgroundColor = color
window.onclick = function() {
    window.document.body.style.backgroundColor = getColor()
}

定时器

  setInterval(function() {
        // 每隔一段时间要执行的代码
    }, 毫秒数)

    返回一个数字,代表当前页面中的第几个定时器 - 利用返回值停止定时器
    clearInterval(返回值)

var timer = setInterval(function() {
    window.document.body.style.backgroundColor = getColor()
}, 1000)

btn.onclick = function() {
    clearInterval(timer)
    console.log(timer);
}

同步和异步

同步:一行代码要执行,必须等待上一行代码执行结束,才能进行 - 以前学的代码都是同步代码

异步:两行代码同时在执行

js是一门单线程语言 - 使用一个内存空间运行代码 - 同一时间只能执行一行代码

进程:内存中预留的空间用来运行软件

线程:比进程更小的内存空间

js中有异步代码,js在执行代码的时候。js是暂时不执行的,会将异步代码交给浏览器处理;

浏览器是多线程的,其中有一个线程 webAPI,专门处理js异步代码 - 等待时间

等待的时间到了 - 将需要执行的代码放在一个队列中 让需要执行的代码等待

当js线程将所有同步代码执行结束了,从队列中依次拿出需要执行的代码执行

结论:异步代码一定会在所有同步代码执行结束以后才会执行

setInterval(function() {
    console.log(1);
}, 1)

for (var a = 1; a <= 10000; a++) {
    console.log(2);
}

双色球案例

// 双色球:6个红球 + 1个蓝球
// 红球:1~31 - 每个数字都不可以重复
// 蓝求:1~16

// 定义数组
var arr = []
// 循环创建6个随机红色号码
for (var a = 0; a < 6; a++) {
    // 生成随机数  getRandom需要调用自己封装生成的随机数
    var num = getRandom(1, 32)
    // 判断num是否在数组中
    var index = arr.indexOf(num)
    if (index >= 0) {
        a--
        continue
    }
    // 将随机数放在数组中
    arr.push(num)
}
// console.log(arr);
// 生成蓝色号码
var lan = getRandom(1, 17)
arr.push(lan)
console.log(arr);
arr.forEach(function(item) {
    document.write('<li>'+item+'</li>');
})

时间日期处理的对象

var date = new Date()
默认在Date的小括号中没有实参 - 就表示当前时间(指的是当前计算机的时间)
非当前时间,需要在小括号中放实参
实参:
    '年-月-日 时:分:秒'

    多个数字   
        年,月,日,时,分,秒

    时间戳:使用毫秒数来描述一个时间的
        从197011000秒开始计算毫秒数
// 注意月份:用0~11来描述 1~12月
var date = new Date(2023,9,1,8,0,0)
console.log(date);

获取具体的时间日期

获取年份

var year = date.getFullYear()
console.log(year);

获取月份

var month = date.getMonth() + 1
console.log(month);

获取日

var d = date.getDate()
console.log(d);

获取星期几

var day = date.getDay()
console.log(day);

获取时

var hour = date.getHours()
console.log(hour);

获取分

var minute = date.getMinutes()
console.log(minute);

获取秒

var second = date.getSeconds()
console.log(second);

获取毫秒

var milisecond = date.getMilliseconds()
console.log(milisecond);

获取时间戳

var time = date.getTime()
console.log(time);

计算100天以前是几月几日案例

// 获取当前时间 - 100天 
var date = new Date()
// 转成时间戳
var time = date.getTime()
// 减去100天的毫秒数
var diff = time - 100 * 24 * 60 * 60 * 1000 // 100天以前的那个时间的时间戳
// 根据结果获取具体的月份和日期 - 依赖时间日期对象
var newDate = new Date(diff)
console.log(newDate);
// 获取月份和日期
var month = newDate.getMonth() + 1;
var d = newDate.getDate()
console.log('100天以前是'+month+'月'+d+'日');

距离国庆还有几天几小时几分钟

// 定义国庆的时间日期对象
var guoqing = new Date('2023-10-1 0:0:0')
// 定义当前的时间日期对象
var now = new Date()
// 求两个时间日期对象对应的时间戳的差
var diff = guoqing.getTime() - now.getTime()
// 计算对应的天、小时、分钟
var day = parseInt(diff / 1000 / 60 / 60 / 24)
console.log(day);
var hour = parseInt(diff / 1000 / 60 / 60) % 24
console.log(hour);
var minute = parseInt(diff / 1000 / 60) % 60
console.log(minute);

设置具体的时间日期

设置月份

date.setMonth(9)
console.log(date);

设置年份

date.setFullYear(2025)
console.log(date);

设置日期

date.setDate(29)
console.log(date);

设置时

date.setHours(23)
console.log(date);

设置分

date.setMinutes(59)
console.log(date);

设置秒

date.setSeconds(59)
console.log(date);

设置时间戳

date.setTime(0)
console.log(date);

封装格式式化时间日期的函数

// 2023年9月14日 星期四 14点38分22秒
var date = new Date()
var guoqing = new Date('2023-10-1 5:21:30')
console.log(dateFormat(date));
console.log(dateFormat(guoqing));
// 函数处理重复
function dateFormat(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var d = date.getDate()
    var arr = ['日', '一', '二', '三', '四', '五', '六']
    var day = date.getDay()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    var str = year + '年' + month + '月' + d + '日 星期' + arr[day] + ' ' + hour + '点' + minute + '分' + second + '秒'
    return str
}