JS_Math和Date知识总结整理

Math 和 Date

  • Math 是 js 的一个内置对象,提供了一堆的方法帮助我们操作 数字
  • Date 是 js 的一个内置对象,提供了一堆的方法帮助我们操作 时间
  • 内置对象就是JavaScript定义好的对象

Math

  • 没有什么多余的东西,就是一堆的方法来操作数字

随机数—random

  • Math.random() 这个方法是用来生成一个 0 ~ 1 之间的随机数

  • 每次执行生成的数字都不一样,但是一定是 0 ~ 1 之间的

  • 生成的数字包含 0 ,但是不包含 1

    var num = Math.random()
    console.log(num) // 得到一个随机数
    
    //生成范围是 min 至 max 的随机整数公式(包括min和max)
    parseInt( Math.random() * ( max + 1 - min ) + min ) ;
    
    //随机整数公式推理(了解即可)
    
    // 生成 1 - 5 的 随机数值
    // 1, 默认范围是0 - 1 不包括1,先乘以5取整是0 - 4
    console.log( parseInt( Math.random()* 5 ) );
    // 2, 先乘以 5+1 取整 0 - 5
    console.log( parseInt( Math.random()* (5+1) ) );
    // 3, 先乘以 5+1 结果是0 - 5, 再加1,结果是 1 - 6
    console.log( parseInt( Math.random()* (5+1) + 1) );
    // 4, 先乘以 5+1-1 结果是 0 - 4, 再加1,结果是 1 - 5
    console.log( parseInt( Math.random()* (5+1-1) + 1) );
    
    // 5 - 10 
    // min 是 5 
    // max 是 10 
    console.log( parseInt( Math.random()*(10+1-5) + 5 ) );
    
    随机数demo
    	浏览器每刷新一次,生成一个随机的颜色。
    	1、rgb类型
        	通过随机数生成 0 - 255 的随机数值 
    	2、字符串颜色类型----# + 6位十六进制数值
        	#后写6位随机的十六进制字符
            十六进制字符范围 0123456789abcdef
    		可以通过[]语法获取某一个字符
    <body>
    <div>我是北京</div>
    <script>
    //	1、rgb类型
    // 通过 DOM操作 给 div标签设定css样式
    const oDiv = document.querySelector('div');
    // rgb 颜色范围是 0 - 255 
    oDiv.style.color = `rgb(${parseInt( Math.random()*256 )},${parseInt( Math.random()*256 )},${parseInt( Math.random()*256 )})`;
    
        
    //2、字符串颜色类型
    // 定义字符串,范围内容是十六进制的字符
    var str = '0123456789abcedf';
    // 定义字符串 存储 随机颜色十六进制字符串
    // 默认值 是 起始字符 #
    var colorStr = '#' ;
    // 循环六次 拼接6个随机字符
    for( var i = 1 ; i <= 6 ; i++ ){
    	colorStr += str[ parseInt( Math.random() * str.length ) ] ;
    }
    console.log( colorStr );
    // 将颜色字符串 设定给标签
    const oDiv = document.querySelector('div');
    // rgb 颜色范围是 0 - 255 
    oDiv.style.color = colorStr;
    </script>
    </body>
    

浮点数取整—round、ceil、floor、toFixed

  • Math.round() 是将一个小数 四舍五入 变成一个整数

    var num = 10.1
    console.log(Math.round(num)) // 10
    
    var num2 = 10.6
    console.log(Math.round(num2)) // 11
    
  • Math.ceil() 是将一个小数 向上取整 得到的整数 (又叫:进一取整)

    // 向上取整 进一取整 向整数部分进位
    console.log( Math.ceil( 123.0000000001 ) );    //=> 124
    console.log( Math.ceil( -123.99999 ) );		   //=> -123
    
  • Math.floor() 是将一个小数 向下取整 的到的整数 (又叫:舍弃取整)

    // 向下取整 舍弃取整 完全舍弃小数部分 效果和parseInt()基本相同
    console.log( Math.floor( 123.9999999999 ) );	//=> 123
    console.log( Math.floor( -123.99999 ) );		//=> -124 
    

向上向下 看 数轴的上下
1
0.9
.
.
.
0.4
0.3 0.3 向上取整是 1 向下取整是 0
0.2
0.1
0
-0.1
-0.2
-0.3 -0.3 向上取整是 0 向下取整是 -1
.
.
.
-0.9
-1

  • 变量.toFixed(小数位数) 设定浮点数转化为字符串时保留的几位小数,四舍五入保留。

    var float = 123.456789;
    var res = float.toFixed( 2 );
    console.log( res );		//=> 123.46
    

数学函数

abs
  • Math.abs() 是返回一个数字的 绝对值

    var num = -10
    console.log(Math.abs(num)) // 10
    
pow
  • Math.pow(参数1 , 参数2) 幂运算函数

    参数1是底数

    参数2是指数

    var res1 = Math.pow( 2 , 5 );
    console.log( res1 );	//=> 32
    
sqrt
  • Math.sqrt() 求平方根,只会输出正的平方根。

    var res1 = Math.sqrt( 9 );
    console.log( res1 );	//=> 3
    
    var res2 = Math.sqrt( -9 );
    console.log( res2 );	//=> NaN
    
max
  • Math.max() 得到的是你传入的几个数字之中 最大 的那个数字

    console.log(Math.max(1, 2, 3, 4, 5)) // 5
    
min
  • Math.min() 得到的是你传入的几个数字之中 最小 的那个数字

    console.log(Math.min(1, 2, 3, 4, 5)) // 1
    
PI
  • Math.PI 得到的是 π 的值,也就是 3.1415936...

    console.log(Math.PI)     //=> 3.141592653589793
    
    • 因为计算机的计算精度问题,只能得到小数点后 15 位
    • 使用 Math.PI 的时候,是不需要加 () 的
SQRT2
  • 2的平方根

     console.log(Math.SQRT2)  //=> 1.4142135623730951
    

数字转换进制

  1. toString() 方法可以在数字转成字符串的时候给出一个进制数

    • 语法: toString(你要转换的进制)

      var num = 100
      console.log(num.toString(2)) // 1100100
      console.log(num.toString(8)) // 144
      console.log(num.toString(16)) // 64
      
  2. parseInt() 方法可以在字符串转成数字的时候把字符串当成多少进制转成十进制

    • 语法: parseInt(要转换的字符串,当作几进制来转换)
    var str = 100
    console.log(parseInt(str, 8)) // 64 把 100 当作一个 八进制 的数字转换成 十进制 以后得到的
    console.log(parseInt(str, 16)) // 256 把 100 当作 十六进制 的数字转换成 十进制 以后得到的
    console.log(parseInt(str, 2)) // 4 把 100 当作 二进制 的数字转换成 十进制 以后得到的
    

Date

  • js 提供的内置构造函数,专门用来获取时间的
  • 创建 时间对象 var time = new Date();

new Date()

  • new Date() 在不传递参数的情况下是默认返回当前时间

    var time = new Date()
    console.log(time) // 当前时间 Fri Mar 01 2019 13:11:23 GMT+0800 (中国标准时间)
    
    

在这里插入图片描述

  • new Date() 在传入参数的时候,可以获取到一个你传递进去的时间

    var time = new Date('2019-03-03 13:11:11')
    console.log(time) // Sun Mar 03 2019 13:11:11 GMT+0800 (中国标准时间)
    
  • new Date() 传递的参数有多种情况

1、传递两个数字,第一个表示年,第二个表示月份
2、 月份从 0 开始计数,0 表示 1月,11 表示 12月

var time = new Date(2019, 00) 
   console.log(time) // Tue Jan 01 2019 00:00:00 GMT+0800 (中国标准时间)

3、传递三个数字,前两个不变,第三个表示该月份的第几天,从 1 到 31

     var time = new Date(2019, 00, 05) 
   console.log(time) // Sat Jan 05 2019 00:00:00 GMT+0800 (中国标准时间)

4、传递四个数字,前三个不变,第四个表示当天的几点,从 0 到 23

   var time = new Date(2019, 00, 05, 22) 
   console.log(time) // Sat Jan 05 2019 22:00:00 GMT+0800 (中国标准时间)

5、传递五个数字,前四个不变,第五个表示的是该小时的多少分钟,从 0 到 59

   var time = new Date(2019, 00, 05, 22, 33) 
   console.log(time) // Sat Jan 05 2019 22:33:00 GMT+0800 (中国标准时间)

6、传递六个数字,前五个不变,第六个表示该分钟的多少秒,从 0 到 59

   var time = new Date(2019, 00, 05, 22, 33, 55) 
   console.log(time) // Sat Jan 05 2019 22:33:55 GMT+0800 (中国标准时间)

7、传入字符串的形式

console.log(new Date('2019')) 
     // Tue Jan 01 2019 08:00:00 GMT+0800 (中国标准时间)
     console.log(new Date('2019-02')) 
     // Fri Feb 01 2019 08:00:00 GMT+0800 (中国标准时间)
     console.log(new Date('2019-02-03')) 
     // Sun Feb 03 2019 08:00:00 GMT+0800 (中国标准时间)
     console.log(new Date('2019-02-03 13:')) 
     // Sun Feb 03 2019 13:00:00 GMT+0800 (中国标准时间)
     console.log(new Date('2019-02-03 13:13:')) 
     // Sun Feb 03 2019 13:13:00 GMT+0800 (中国标准时间)
     console.log(new Date('2019-02-03 13:13:13')) 
     // Sun Feb 03 2019 13:13:13 GMT+0800 (中国标准时间)

8、总结:

1,	获取当前时间
	var 变量 =  new Date() ;
2,  获取指定时间
方法一:(在项目中推荐使用, 不用考虑月份加不加以1的问题)
	var 变量 = new Date('时间字符串') ;
		'年 月 日 时:分:秒'
		'年-月-日 时:分:秒'
		'年/月/日 时:分:秒'
		'年,月,日 时:分:秒'
	设定的时间数值, 必须要在正常值范围之内
		月份的范围是 1 - 12
		日期的范围是 1 - 31
		小时的范围是 0 - 24
		分钟的范围是 0 - 59
		秒的范围是   0 - 59
方法二:
    var 变量 = new Date(年,月,日,小时,分钟,秒);
		写 6个时间’数字‘
		月份设定 0 - 11 的数值, 对应的 1 - 12 的月份
		数字可以超出正常值范围, 按照计算进位
注:方法一与方法二区别:
	获取月份是不同,获取周的时候方法二不行。
	除此之外获取的都相同。
3,	周不用给, 会根据你给的定时间自动算出。
	但是方法一可以正确计算出周,方法二算出的周几是错误的。

将日期字符串格式化成指定内容

  • 比如我们得到的时间字符串是 Sun Feb 03 2019 13:13:13 GMT+0800 (中国标准时间)

  • 之前如果我们要得到这个日期中的年,我们要靠截取字符串的形式得到

  • 但是现在 js 为我们提供了一系列的方法来得到里面的指定内容

getFullYear
  • getFullYear() 方式是得到指定字符串中的哪一年,4位年份

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getFullYear()) // 2019
    
    var time = new Date('2019, 03, 03 08:00:22')
    console.log(time.getFullYear()) // 2019
    
    // 获取当前时间是哪一年
    var time = new Date();
    var year = time.getFullYear();
    console.log( year )
    
getMonth
  • getMonth() 方法是得到指定字符串中的哪一个月份

    var time = new Date(2019, 03, 03, 08, 00, 22);
    console.log(time);	
    //用这个方法获取的时间是Wed Apr 03 2019 08:00:22 GMT+0800 (中国标准时间)
    //Apr是4月, 而getMonth()获取的是 0 - 11 的数值,所以getMonth()输出的是3,这个方法用getMonth()能直接获取正确的月份,而不用加1
    console.log(time.getMonth());		 //=> 3
    
    var time = new Date('2019, 03, 03 08:00:22')
    console.log(time);	
    //用字符串形式获取指定时间,能够正常获取时间,但是获取月份时需要加1
    //Sun Mar 03 2019 08:00:22 GMT+0800 (中国标准时间)
    console.log(time.getMonth());	//=> 2
    
    var time1 = new Date();
    // 获取的结果是0 - 11的数值, 对应的1 - 12月份
    // 也就是获取结果 + 1 是对应的月份的数值
    var month = time1.getMonth() + 1 ;
    // 前导补零
    month = month < 10 ? '0' + month : month;
    console.log( month );
    
    // 可以定义一个数组, 存储一至十二的中文
    var monthArr = ['一','二','三','四','五','六','七','八','九','十','十一','十二'] ;
    // 获取的time.getMonth()是0 - 11的数值, 正好是数组中对应中文的索引下标
    var month = monthArr[ time1.getMonth() ];
    console.log( month );
    
    • 这里要有一个注意的地方
    • 月份是从 0 开始数的
    • 0 表示 1月,1 表示 2月,依此类推
getDate
  • getDate() 方法是得到指定字符串中的哪一天

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getDate()) // 3
    
    var time = new Date('2019, 03, 03 08:00:22')
    console.log(time.getDate())	//	3
    
    // 获取当前时间是哪一天
    var time = new Date();
    var day = time.getDate();
    // 前导补零
    day = day < 10 ? '0' + day : day;
    console.log( day );
    
getHours
  • getHours() 方法是得到指定字符串中的哪小时

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getHours()) // 8
    
    var time = new Date('2019, 03, 03 08:00:22')
    console.log(time.getHours())	// 8
    
    // 获取当前时间是哪一小时
    var time = new Date();
    var hours = time.getHours();
    // 前置补零
    // 给变量hours赋值, 如果hours本身小于10, 在hours存储的数值前拼接字符串'0', 赋值给变量hours存储
    // 如果hours本身不小于10, hours赋值存储本身的数值
    hours = hours < 10 ? '0' + hours : hours ;
    console.log( hours );
    
getMinutes
  • getMinutes() 方法是得到指定字符串中的哪分钟

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getMinutes()) // 0
    
    var time = new Date('2019, 03, 03 08:00:22')
    console.log(time.getMinutes())	// 0
    
    // 获取当前时间是哪一分钟
    var time = new Date();
    var minutes = time.getMinutes();
    // 前置补零
    minutes = minutes < 10 ? '0' + minutes : minutes ; 
    console.log( minutes );
    
getSeconds
  • getSeconds() 方法是得到指定字符串中的哪秒钟

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getSeconds()) // 22
    
    var time = new Date('2019, 03, 03 08:00:22')
    console.log(time.getSeconds())	// 22
    
    // 获取当前时间是哪一秒
    var time = new Date();
    // 获取秒
    var seconds = time.getSeconds();
    // 前置补零
    seconds = seconds < 10 ? '0' + seconds : seconds ;
    console.log( seconds );
    
getDay
  • getDay()方法是得到指定字符串当前日期是一周中的第几天(周日是 0,周六是 6)

    var time = new Date(2021, 11, 27, 15, 34, 22);
    console.log(time.getDay()) 		// 1	(用这种方法算出的周不准确)
    var time = new Date('2021, 11, 27, 15:34:22')
    console.log(time.getDay())		// 6	(用这种方法能准确算出周)
    
    // 获取今天是星期几
    var time = new Date();
    // 获取结果是0 - 6的数字, 对应的星期日 - 星期六
    var week = time.getDay();
    console.log( week );
    
    // 创建一个数组, 使用time.getDay()作为索引下标获取对应的中文
    var weekArr = ['星期日' , '星期一' , '星期二' , '星期三' , '星期四' , '星期五' , '星期六' ];
    var week = weekArr[ time.getDay() ];
    console.log( week );
    
getTime
  • getTime() 方法是得到 执行时间格林威治时间 的毫秒数
var time = new Date(2019, 03, 08, 08, 00, 22)
console.log(time.getTime()) // 1554681622000
  • 时间戳 是 当前时间 距离 世界标准时间/格林尼治时间 1970年1月1日 0点0分0秒 的时间差, JavaScript中时间戳单位是毫秒。
//获取时间戳
/*
	1,  创建 时间对象
    	var 时间对象 = new Date() ;
	2,  获取 时间戳
		var 变量 = 时间对象.getTime() ;
         单位是 毫秒 
         1000 毫秒 =  1 秒
		转化为 秒 就是 / 1000 取整
*/
var nowTime = new Date() ; 
// 获取当前的时间戳
var res1 = parseInt( nowTime.getTime() / 1000 );
console.log( res1 );

获取时间差

获取时间差是指获取两个时间点之间相差的时间
	在 js 中是不能用时间直接做 减法 的, 我们需要一些特殊的操作, 在编程的世界里面, 有一个特殊的时间, 是 `1970年01月01日00时00分00秒`
	这个时间我们叫做 `格林威治时间`, 所有的编程世界里面, 这个时间都是一样的, 而且 `格林威治时间` 的数字是 0, 从 `格林威治时间` 开始, 每经过1毫秒, 数字就会 + 1, 所以我们可以获取到任意一个时间节点到 `格林威治时间` 的毫秒数
, 然后用两个毫秒数相减, 就能得到两个时间点之间相差的毫秒数, 我们在通过这个毫秒数得到准确的时间。
计算时间差
  • 例如:我们现在计算一下距离元旦2022-1-1 0:0:0 的时间差。
/*
	时间戳demo
	距离元旦放假 2022-1-1 0:0:0 的时间差
*/

//  创建当前时间对象
var time1 = new Date() ; 
        
//  创建元旦时间对象
var time2 = new Date('2022-1-1 0:0:0');

//  获取时间戳, 两个时间戳相减, 结果转化为秒
var time = parseInt( ( time2.getTime() - time1.getTime() ) / 1000 ) ;
console.log( time );	//=> 2887530

// 转化为天\小时\分钟\秒, 前导补零
var d = parseInt( time / (24*60*60) );
d = d < 10 ? '0' + d : d ;
 
var h = parseInt( ( time % (24*60*60) ) / (60*60) );
h = h < 10 ? '0' + h : h ;

var m = parseInt( ( time % (60*60) ) / 60 );
m = m < 10 ? '0' + m : m ;

var s = time % 60 ;
s = s < 10 ? '0' + s : s ;

//输出
console.log( d , h , m , s );	
//=> 33 10 '05' 30	(里面若是前导补零的话,格式是字符串类型)

设定时间数据

通过内置构造函数可以创建是一个时间对象 
	时间对象中存储创建时的时间或者设定的时间
	可以通过函数方法设定时间对象中存储的时间数据
// time 中存储的是创建时间对象时的时间数据
var time = new Date();
console.log(time);	
//=> Sun Nov 28 2021 14:06:43 GMT+0800 (中国标准时间)

// 设定年
time.setFullYear(2002);

// 设定月份
// 设定的数值是 0-11 的数字, 对应 1-12 的月份
time.setMonth(1);

// 设定日期
time.setDate(25);

// 星期是根据年月日计算的结果, 不能设定

// 设定小时
time.setHours(8);

// 设定分钟
time.setMinutes(8);

// 设定秒
time.setSeconds(8);

//输出设定后的时间
console.log(time);	
//=> Mon Feb 25 2002 08:08:08 GMT+0800 (中国标准时间)


*******************************************************************************************
// ***设定时间戳就是设定所有的时间
//上面的方法基本不用,用下面这个
// 例如需要设定3天后的时间, 则用当前时间的时间戳 + 3天总共的毫秒数, 将结果作为新的时间戳设定给时间对象****
var time = new Date();
time.setTime( time.getTime() + 3*24*60*60*1000 )  ;
console.log( time );
//=> Wed Dec 01 2021 14:08:45 GMT+0800 (中国标准时间)

//设定3天前的时间, 则用当前时间的时间戳 - 3天总共的毫秒数, 将结果作为新的时间戳设定给时间对象
var time = new Date();
time.setTime( time.getTime() - 3*24*60*60*1000 )  ;
console.log( time );
//=> Thu Nov 25 2021 14:12:58 GMT+0800 (中国标准时间)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值