数学处理和时间日期处理

275 阅读8分钟

一、数学处理

js内部提供了一个对象,专门用于做数学操作

console.log( Math );

随机数 - random()

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

20~100之间的随机整数

console.log( parseInt(Math.random() * 81) + 20 );

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);

一些简单的求法

// 向上取整的含义是一个数字的小数部分不够1,将他处理成1。例如:10条数据每页展示3条,前3页都能放3条数据,但是第4页只能放1条数据,虽然占不满1页,但也要占1页

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

// 向下取整 - 向下取整跟parseInt()是一个意思,只要整数部分,舍掉小数部分得到整数

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

// 四舍五入 -

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

// 求最大值 -

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) );

生成的是0~1之间的随机小数,通常在实际项目中需要获取到一个范围内的随机整数,利用这个随机小数封装一个获取范围内的随机整数的函数:

```
function getRandom(a,b){
    var max = a;
    var min = b;
    if(a<b){
        max = b;
        min = a;
    }
    return parseInt(Math.random() * (max - min)) + min
}
```

最常用的:

Math.random()
Math.ceil()
Math.max()
Math.min()

2、进制的转换

10进制转其他进制:10进制数字.toString(进制数)

var x = 110;
x.toString(2) // 转为2进制
x.toString(8) // 转为8进制
x.toString(16) // 转为16进制

数字.toString(目标进制数)

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

// var a = 9
// console.log( a.toString(8) );

// var a = 15
// console.log( a.toString(16) );

其他进制转10进制:parseInt(数据,进制数)

var x = "110" // 这是一个二进制的字符串表示
parseInt(x, 2) // 把这个字符串当做二进制, 转为十进制var x = "70" // 这是一个八进制的字符串表示
parseInt(x, 8) // 把这个字符串当做八进制, 转为十进制var x = "ff" // 这是一个十六进制的字符串表示
parseInt(x, 16) // 把这个字符串当做十六进制, 转为十进制
var a = '11'
// console.log( parseInt(a, 2) );
// console.log( parseInt(a, 8) );
// console.log( parseInt(a, 16) );

定时器:让一段代码每隔一段时间就执行一次,不停的执行下去

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

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

clearInterval(返回值)
var a = 1
// setInterval(function() {
//     a++
//     console.log(a);
// }, 1000)
// setInterval(function() {
//     window.document.body.style.backgroundColor = getColor()
// }, 1000)
// var timer = setInterval(function() {
//     window.document.body.style.backgroundColor = getColor()
// }, 1000)
// btn.onclick = function() {
//     clearInterval(timer)
//     console.log(timer);
// }
var a = 100
var timer = setInterval(function() {
    a++
    console.log(a);
}, 1000)
console.log(timer);

定时器是异步代码

同步和异步

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

// console.log(1);
// console.log(2);

异步:两行代码同时在执行 js是一门单线程语言 - 使用一个内存空间运行代码 - 同一时间只能执行一行代码 进程:内存中预留的空间用来运行软件 线程:比进程更小的内存空间

js中有异步代码,js在执行代码的时候

一次将同步代码执行,当碰到异步代码的时候,js是暂时不执行的,会将异步代码交给浏览器处理 浏览器是多线程的,其中有一个线程 webAPI,专门处理js异步代码 - 等待时间 等待的时间到了 - 将需要执行的代码放在一个队列中 让需要执行的代码等待 当js线程将所有同步代码执行结束了,从队列中依次拿出需要执行的代码执行

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

定时器的时间不精准

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

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

二、时间日期处理

js提供了一个构造函数Date,用来创建时间日期对象。所以跟时间日期有关的操作都是通过时间日期对象来操作的。

new Date()
var date = new Date()
// 我们在输出这个对象的时候,他内部会自动转成字符串输出 - 因为
// console.log(date);
// console.log(typeof date);

默认在Date的小括号中没有实参 - 就表示当前时间(指的是当前计算机的时间) 如果我们希望得到非当前时间,需要在小括号中放实参 实参: '年-月-日 时:分:秒' 多个数字 年,月,日,时,分,秒

时间戳:使用毫秒数来描述一个时间的 从1970年1月1日0点0分0秒开始计算毫秒数

 var date = new Date('2023-10-1 08:00:00')
console.log(date);

注意月份:用011来描述 112月

var date = new Date(2023,9,1,8,0,0)
console.log(date);

当前时间的时间日期对象

var date = new Date()
console.log(date) // Tue Jul 30 2019 21:26:56 GMT+0800 (中国标准时间)

创建好的是一个对象,但是当输出的时候被浏览器自动转为字符串输出了。获取到的是当前本地的时间日期对象。如果把本地的时间日期改掉,获取到的时间日期对象会随着本地时间变化。

指定的时间日期对象

var date = new Date("年-月-日 时:分:秒") // 也可以是英文版的时间字符串 - Sun May 13,2016
var date = new Date(年,月-1,日,时,分,秒)
var date = new Date(时间戳)

2、获取具体的时间日期

通过时间日期对象可以获取到具体的年月日时分秒,甚至毫秒和时间戳。

date.getFullYear(); // 获取到完整的时间日期对象中的年份
date.getMonth(); // 获取到时间日期对象中的月份 - 这里的月份是通过0~11来描述1~12月的
date.getDate(); // 获取到时间日期对象中的日
date.getDay(); // 获取时间日期对象中的星期几
date.getHours(); // 获取到时间日期对象中的时
date.getMinutes(); // 获取到时间日期对象中分
date.getSeconds(); // 获取到时间日期对象中的秒
date.getMilliseconds(); // 获取到时间日期对象中的毫秒 - 1秒=1000毫秒
date.getTime(); // 获取到时间日期对象对应的时间戳

时间戳,指的是,格林尼治时间1970年1月1日0点0分0秒到现在走过的毫秒数。利用时间戳可以方便计算时间,例如:计算100天以前是几月几号。

将年月日时分秒放在页面中显示:

var date = new Date();
var year = date.getFullYear(); 
var month = date.getMonth()+1;
var d = date.getDate();
var day = date.getDay();
var hour = date.getHours(); 
var minute = date.getMinutes(); 
var second = date.getSeconds(); 
document.write("现在是北京时间:"+year+"年"+month+"月"+d+"日。星期"+day+"。"+hour+"时"+minute+"分"+second+"秒");

2、设置时间日期

通过时间日期对象,可以将其中的年月日时分秒进行设置,改变时间日期对象的时间。

date.setFullYear(年份); // 设置时间日期对象中的年份
date.setMonth(当前月份-1); // 设置时间日期对象中的月份 - 这里的月份是通过0~11来描述1~12月的
date.setDate(日); // 设置时间日期对象中的日
date.setHours(时); // 设置时间日期对象中的时
date.setMinutes(分); // 设置时间日期对象中分
date.setSeconds(秒); // 设置时间日期对象中的秒
date.setMilliseconds(毫秒); // 设置时间日期对象中的毫秒
date.setTime(时间戳); // 设置时间日期对象对应的时间戳 - 直接用时间戳就能将时间日期对象中的年月日时分秒全部改变

星期几是不能设置的,是根据年月日生成的。

3、日期格式化

date.toLocalString();//本地风格的日期格式
date.toLocaleDateString(); // 获取日期
date.toLocaleTimeString(); // 获取时间

4、时间戳的获取

格林威治时间/格林尼治时间

Date.parse("2015-08-24") // 获取1970年到设定时间的毫秒数
new Date().getTime()
+new Date();

案例:

两个指定的日期相差多少天

var date1=new Date(2010,10,3);
var date2=new Date(2017,9,24);
var day=(date2.getTime()-date1.getTime())/(1000*60*60*24);/*不用考虑闰年否*/
console.log("相差"+day+"天");

100天以后是哪年哪月哪日

var date = +new Date()
date += 100 * 24 * 3600 * 1000
var newDate = new Date(date)
var year = newDate.getFullYear()
var month = newDate.getMonth() + 1;
var d = newDate.getDate()
console.log('100天以后是'+year+'年'+month+'月'+d+'日')