Math对象_静态属性
学习效果反馈
Math对象_静态方法一
Math.abs()
Math.max(),Math.min()
常见应用场景
Math.floor(),Math.ceil()
学习效果反馈
Math对象_静态方法二
Math.round()
Math.pow()
Math.sqrt()
Math.log()
Math.exp()
学习效果反馈
Math对象_静态方法三
Math.random()
三角函数方法
学习效果反馈
Date对象
普通函数的用法
构造函数的用法
学习效果反馈
Date对象_静态方法
时间戳
Date.now()
Date.parse()
学习效果反馈
Date对象_实例方法/to类
Date.prototype.toUTCString()
Date.prototype.toDateString()
Date.prototype.toTimeString()
Date.prototype.toLocaleDateString()
Date.prototype.toLocaleTimeString()
学习效果反馈
Date对象_实例方法/get类
学习效果反馈
Date对象_实例方法/set类
学习效果反馈
Math与Date实操
学习效果反馈
JS时间戳、日期互相转换
引入时间戳,生成可控长度的随机数
学习效果反馈
Math对象_静态属性
Math是 JavaScript 的原生对象,提供各种数学功能。该对象不是构 造函数,不能生成实例,所有的属性和方法都必须在Math对象上调用
Math对象的静态属性,提供以下一些数学常数
Math.E:常数e。
Math.LN2:2 的自然对数。
Math.LN10:10 的自然对数。
Math.LOG2E:以 2 为底的e的对数。
Math.LOG10E:以 10 为底的e的对数。
Math.PI:常数 PI。
Math.SQRT1_2:0.5 的平方根。
Math.SQRT2:2 的平方根。
Math.E // 2.718281828459045
Math.LN2 // 0.6931471805599453
Math.LN10 // 2.302585092994046
Math.LOG2E // 1.4426950408889634
Math.LOG10E // 0.4342944819032518
Math.PI // 3.141592653589793
Math.SQRT1_2 // 0.7071067811865476
Math.SQRT2 // 1.4142135623730951
实现圆面积
function circleArea(r){
return Math.PI * r * r;
}
circleArea(10); // 314.1592653589793
学习效果反馈
1. 下列计算圆面积代码,横线处应该填写的内容是:
function circleArea(r){ return ___ * r * r; } circleArea(10); // 314.1592653589793
A Math.E
B Math.SQRT2
C Math.LN2
D Math.PI
Math对象_静态方法一
Math.abs()
Math.abs 方法返回参数值的绝对值
Math.abs(1) // 1
Math.abs(-1) // 1
Math.max(),Math.min()
Math.max 方法返回参数之中最大的那个值, Math.min 返回最小的那个
值。如果参数为空, Math.min 返回 Infinity , Math.max 返回 -Infinity 。
Math.max(2, -1, 5) // 5
Math.min(2, -1, 5) // -1
Math.min() // Infinity
Math.max() // -Infinity
常见应用场景
利用Math的最大值和最小值求数组的最大值最小值
Math.min.apply(null,[10,20,30,40])
Math.floor(),Math.ceil()
Math.floor 方法返回小于参数值的最大整数
Math.floor(3.2) // 3
Math.floor(-3.2) // -4
Math.ceil 方法返回大于参数值的最小整数
Math.ceil(3.2) // 4
Math.ceil(-3.2) // -3
这两个方法可以结合起来,实现一个总是返回数值的整数部分的函数
function ToInteger(x) {
x = Number(x);
return x < 0 ? Math.ceil(x) :
Math.floor(x);
}
ToInteger(3.2) // 3
ToInteger(3.5) // 3
ToInteger(3.8) // 3
ToInteger(-3.2) // -3
ToInteger(-3.5) // -3
ToInteger(-3.8) // -3
学习效果反馈
1. 下列代码获得一个正整数,横线处应该填写的内容是:
function ToInteger(x) { x = Number(x); return ___(___(x)); } ToInteger(-10.4); // 向下取整:10
A Math.floor Math.abs
B Math.ceil Math.abs
C Math.ceil Math.min
D Math.floor Math.min
2. 下列代码获得一个数组最大值,横线处应该填写的内容是:
Math.___.apply(null,[10,20,30,40])
A ceil
B floor
C max
D min
Math对象_静态方法二
Math.round()
Math.round 方法用于四舍五入
Math.round(0.1) // 0
Math.round(0.5) // 1
Math.round(0.6) // 1
Math.round(-1.1) // -1
Math.round(-1.5) // -1
Math.round(-1.6) // -2
Math.pow()
Math.pow 方法返回以第一个参数为底数、第二个参数为幂的指数值
// 等同于 2 ** 2
Math.pow(2, 2) // 4
// 等同于 2 ** 3
Math.pow(2, 3) // 8
下面是计算圆面积的方法
var radius = 20;
var area = Math.PI * Math.pow(radius, 2);
Math.sqrt()
Math.sqrt 方法返回参数值的平方根。如果参数是一个负值,则返回 NaN
Math.sqrt(4) // 2
Math.sqrt(-4) // NaN
Math.log()
Math.log 方法返回以 e 为底的自然对数值
Math.log(Math.E) // 1
Math.log(10) // 2.302585092994046
Math.exp()
Math.exp 方法返回常数 e 的参数次方
Math.exp(1) // 2.718281828459045
Math.exp(3) // 20.085536923187668
学习效果反馈
1. 下列代码计算圆的面积,横线处应该填写的内容是:
var radius = 20; var area = ___ * ___(radius, 2);
A Math.PI Math.pow
B Math.pow Math.PI
C Math.pow Math.sqrt()
D Math.PI Math.sqrt()
2. 下列那个代码是平方根的数学函数:
A Math.PI
B Math.pow
C Math.sqrt()
D Math.lo
Math对象_静态方法三
Math.random()
Math.random() 返回0到1之间的一个伪随机数,可能等于0,但是一定小于1
Math.random() // 0.28525367438365223
任意范围的随机数生成函数如下
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
getRandomArbitrary(5, 10)
返回随机字符实例
function random_str(length) {
var ALPHABET =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
ALPHABET += 'abcdefghijklmnopqrstuvwxyz';
ALPHABET += '0123456789-_';
var str = '';
for (var i=0; i < length; ++i) {
var rand = Math.floor(Math.random() * ALPHABET.length);
str += ALPHABET.substring(rand, rand + 1);
}
return str;
}
random_str(6) // "GEghrr"
三角函数方法
Math 对象还提供一系列三角函数方法
Math.sin():返回参数的正弦(参数为弧度值)
Math.cos():返回参数的余弦(参数为弧度值)
Math.tan():返回参数的正切(参数为弧度值)
Math.asin():返回参数的反正弦(返回值为弧度值)
Math.acos():返回参数的反余弦(返回值为弧度值)
Math.atan():返回参数的反正切(返回值为弧度值)
学习效果反馈
1. 下列代码返回一个随机字符串,横线处应该填写的内容是:
function random_str(length) { var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; ALPHABET += 'abcdefghijklmnopqrstuvwxyz'; ALPHABET += '0123456789-_'; var str = ''; for (var i=0; i < length; ++i) { var rand = Math.floor(___ * ALPHABET.length); str += ALPHABET.___(rand, rand + 1); } return str; } random_str(6)
A Math.random() substring
B Math.random() Math.PI
C Math.round substring)
D Math.round Math.sqrt()
Date对象
Date 对象是 JavaScript 原生的时间库。它以1970年1月1日00:00:00 作为时间的零点,可以表示的时间范围是前后各1亿天(单位为毫秒)
普通函数的用法
Date 对象可以作为普通函数直接调用,返回一个代表当前时间的字符串
Date()
'Tue Oct 26 2022 10:31:34 GMT+0800 (中国标准时间)'
Date(2000, 1, 1)
// 'Tue Oct 26 2022 10:31:34 GMT+0800 (中国标准时间)'
构造函数的用法
Date 还可以当作构造函数使用。对它使用 new 命令,会返回一个 Date 对象的实例。如果不加参数,实例代表的就是当前时间
var today = new Date();
作为构造函数时, Date 对象可以接受多种格式的参数,返回一个该参数对应的时间实例
// 参数为时间零点开始计算的毫秒数
new Date(1635215676627)
// Tue Oct 26 2021 10:34:36 GMT+0800 (中国标准时间)
// 参数为日期字符串
new Date('January 6, 2020');
// Mon Jan 06 2020 00:00:00 GMT+0800 (中国标准时间)
// 参数为多个整数,
// 代表年、月、日、小时、分钟、秒、毫秒
new Date(2020, 0, 1, 0, 0, 0, 0)
// Wed Jan 01 2020 00:00:00 GMT+0800 (中国标准时间)
各种类型的参数
new Date('2022-2-15')
new Date('2022/2/15')
new Date('02/15/2022')
new Date('2022-FEB-15')
new Date('FEB, 15, 2022')
new Date('FEB 15, 2022')
new Date('Feberuary, 15, 2022')
new Date('Feberuary 15, 2022')
new Date('15 Feb 2022')
new Date('15, Feberuary, 2022')
日期的运算
var d1 = new Date(2000, 2, 1);
var d2 = new Date(2000, 3, 1);
d2 - d1
// 2678400000 毫秒
学习效果反馈
1. 下列代码关于日期代码运行结果是:
var d1 = new Date("02/15/2022"); var d2 = new Date("2022/03/15"); console.log(d2 - d1);
A 01/00
B -2419200000
C 2419200000
D -01/00
Date对象_静态方法
时间戳
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间 1970年01月01日08时00分00秒)起至现在的总秒数。 格林威治和北京时间就是时区的不同 Unix是20世纪70年代初出现的一个操作系统,Unix认为1970年1月 1日0点是时间纪元。JavaScript也就遵循了这一约束
Date.now()
Date.now 方法返回当前时间距离时间零点(1970年1月1日 00:00:00 UTC)的毫秒数,相当于 Unix 时间戳乘以1000
Date.now(); // 1635216733395
Date.parse()
Date.parse 方法用来解析日期字符串,返回该时间距离时间零点 (1970年1月1日 00:00:00)的毫秒数
Date.parse('Aug 9, 2022')
Date.parse('January 26, 2022 13:51:50')
Date.parse('Mon, 25 Dec 2022 13:30:00 GMT')
Date.parse('Mon, 25 Dec 2022 13:30:00 +0430')
Date.parse('2022-10-10')
Date.parse('2022-10-10T14:48:00')
如果解析失败,返回 NaN
Date.parse('xxx') // NaN
学习效果反馈
1. 使用Date.now()获得时间戳:
A Date.now()
B Date.now() /10000
C Date.now() /100
D Date.now() /1000
Date对象_实例方法/to类
Date 的实例对象,有几十个自己的方法,分为三类
Date.prototype.toUTCString()
toUTCString 方法返回对应的 UTC 时间,也就是比北京时间晚8个小时
var d = new Date(2022, 0, 1);
d.toUTCString()
// "Fri, 31 Dec 2021 16:00:00 GMT"
Date.prototype.toDateString()
toDateString 方法返回日期字符串(不含小时、分和秒)
var d = new Date(2022, 0, 1);
d.toDateString() // "Sat Jan 01 2022"
Date.prototype.toTimeString()
toTimeString 方法返回时间字符串(不含年月日)
var d = new Date(2022, 0, 1);
d.toTimeString() // 00:00:00 GMT+0800 (中国标准时间)
Date.prototype.toLocaleDateString()
toLocaleDateString 方法返回一个字符串,代表日期的当地写法(不含小时、分和秒)
var d = new Date(2022, 0, 1);
d.toLocaleDateString() // 2022/1/1
Date.prototype.toLocaleTimeString()
toLocaleTimeString 方法返回一个字符串,代表时间的当地写法(不含年月日)、
var d = new Date(2022, 0, 1);
d.toLocaleTimeString() // 上午12:00:00
学习效果反馈
1. 下列代码,运行结果是:
var d = new Date(2022, 0, 1); d.toLocaleTimeString()
A Sat Jan 01 2022
B 上午12:00:00
C 2022/1/1
D 00:00:00 GMT+0800 (中国标准时间)
Date对象_实例方法/get类
Date 对象提供了一系列 get* 方法,用来获取实例对象某个方面的值
var d = new Date('January 6, 2022');
d.getDate() // 6
d.getMonth() // 0
d.getYear() // 122
d.getFullYear() // 2022
编写函数获得本年度剩余天数
function leftDays() {
var today = new Date();
var endYear = new Date(today.getFullYear(), 11, 31, 23, 59, 59, 999);
var msPerDay = 24 * 60 * 60 * 1000;
return Math.round((endYear.getTime() - today.getTime()) / msPerDay);
}
学习效果反馈
1. 下列代码计算本年度剩余天数,划横线处应该填写代码是:
function leftDays() { var today = new Date(); var endYear = new Date(today.___, 11, 31,23, 59, 59, 999); var msPerDay = 24 * 60 * 60 * 1000; return Math.round((endYear.___ - today.___) / msPerDay); }
A getTime() getDate() getTime()
B getTime() getTime() getDate()
C getFullYear() getTime() getDate()
D getFullYear() getTime() getTime()
Date对象_实例方法/set类
Date 对象提供了一系列 set* 方法,用来设置实例对象的各个方面
var d = new Date ('January 6, 2022');
d // Thu Jan 06 2022 00:00:00 GMT+0800 (中国标准时间)
d.setDate(9)
d // Sun Jan 09 2022 00:00:00 GMT+0800 (中国标准时间)
var d = new Date();
// 将日期向后推1000天
d.setDate(d.getDate() + 1000);
// 将时间设为6小时后
d.setHours(d.getHours() + 6);
// 将年份设为去年
d.setFullYear(d.getFullYear() - 1);
学习效果反馈
1. 下列代码,划横线处应该填写代码是:
var d = new Date(); var year = d.___; if(year % 4 === 0 && year % 100 != 0 || year % 400 === 0){ console.log(d.setDate(d.___ + 366));; }else{ console.log(d.setDate(d.getDate() + 365));; }
A getFullYear() getDate()
B getFullYear() getDay()
C getYear() getDate()
D getYear() getDay()
Math与Date实操
编写一个函数,获得一个十六进制的随机颜色的字符串(例 如:#20CD4F)方法
function genColor(){
var str="0123456789abcdef";
var color="#";
for(var i=0;i<6;i++){
var num=Math.floor(Math.random()*str.length);
color=color+str[num];
}
return color;
}
学习效果反馈
1. 下列代码,划横线处应该填写代码是:
function genColor(){ var str="0123456789abcdef"; var color="#"; for(var i=0;i<6;i++){ var num=___(___*str.length); color=color+str[num]; } return color; }
A Math.floor Math.round()
B Math.ceil Math.round()
C Math.ceil Math.random()
D Math.floor Math.random()
JS时间戳、日期互相转换
// 时间戳转日期
function timestampToTime(timestamp) {
var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = date.getDate() + ' ';
var h = date.getHours() + ':';
var m = date.getMinutes() + ':';
var s = date.getSeconds();
return Y + M + D + h + m + s;
}
// 日期转时间戳
var date = new Date('2020-01-01 18:55:49:123');
var time1 = date.getTime();
引入时间戳,生成可控长度的随机数
function genID(length){
return Math.random().toString().substr(2,length) + Date.now().toString(36);
}
学习效果反馈
1. 下列代码,划横线处应该填写代码是:
function genID(length){ return ___.toString().___(2,length) + Date.now().toString(36); }
A Math.round() split
B Math.random() split
C Math.random() substr
D Math.round() substr