6.5 date math对象的学习
* 21.Math对象
* 22.Date对象
* 23.错误处理
Math对象
Math对象:仅专门提供数学计算的方法
Math对象没有构造函数,所以不能new!——唯一一个
所有API都直接通过Math类型名调用
- 取整:
上取整:只要超过,就取下一个整数
Math.ceil(num)
下取整:去掉小数部分,取整数部分
Math.floor(num)
四舍五入取整:小数部分够5就进1,不够就舍掉
var num=Math.round(num)
缺点:只能取整
var str=num.toFixed(d); d:0-20之间
按指定d位小数四舍五入
缺点:返回字符串,不能直接用于计算
自定义round方法:3步:
function round(num,d){
num*10的d次方倍
Math.round(num.toFixed(2))
num/10的d次方倍
返回number
}
var math1 = 1.39;
var math2 = 1.99;
var math3 = 1.09;
console.log("向上取整");
console.log(Math.ceil(math1)+":"+Math.ceil(math2)+":"+Math.ceil(math3));
console.log("向下取整");
console.log(Math.floor(math1)+":"+Math.floor(math2)+":"+Math.floor(math3));
console.log("四舍五入取整");
console.log(Math.round(math1)+":"+Math.round(math2)+":"+Math.round(math3));
var math4 = 1.3935721;
console.log("按指定位小数四舍五入");
console.log(math4.toFixed(4)); //1.3936
2.乘方和开平方
乘方: Math.pow(底数,幂)
开平方: Math.sqrt(num);
console.log(Math.pow(8,2)); //64
console.log(Math.sqrt(25)); //5
3.最大值和最小值
Math.max(值1,值2,...)
Math.min(值1,值2,...)
如何获取数组中的最大值:固定套路
Math.max.apply(Math,arr);
var arr = [7,9,3,5,1,2];
console.log(Math.max.apply(Math,arr));
随机数:Math.random() 0<=r<1的小数
任意min~max之间取随机整数
Math.floor(Math.random()(max-min+1)+min)
如果min从0开始,可简写为:
Math.floor(Math.random()(max+1))
var min = 10;
var max = 20;
var rs = Math.floor(Math.random()*(max-min+1)+min); //min从0开始时可以缩写
console.log(rs);
Date对象
Date对象:封装一个时间值,提供对时间的操作方法
Date对象中封装了1970年1月1日0点至今的毫秒数
何时使用:计算日期前,都要创建或获得日期对象
创建:4种:
- 创建日期对象,同时获得客户端当前时间
var now=new Date();
var now = new Date();//当前日期时间
console.log(now);
var ms = now.getTime(); //1970年1月1日 至 今天毫秒数
console.log(ms);
2.var date=new Date("yyyy/mm/dd[ hh:MM:ss]");自定义时间点
var date=new Date(yyyy,mm,dd,hh,MM,ss);
date对象中,月从0~11
现实中月要-1修改
从日期对象取出月都+修改
var now = new Date("2111/11/11 11:11:11");
console.log(now);
var now = new Date(2222,11,12,12,12,12);
console.log(now); //Thu Dec 12 2222 12:12:12 GMT+0800 (中国标准时间)
3.复制一个日期对象:在计算之前,将旧日期对象中的毫秒数取出,放入一个新的日期对象中保存副本。
所有日期API都直接修改原日期对象,无法保留旧值
var old=new Date("yyyy/mm/dd");
var newDate=new Date(old.getTime());
结果:修改newDate,old不会受影响
var newDate = new Date(now.getTime());
console.log(newDate); //Thu Dec 12 2222 12:12:12 GMT+0800 (中国标准时间)
Date对象API:年 月 日 星期
FullYear Month Date Day
时 分 秒 毫秒
Hours Minutes Seconds Milliseconds
1.每个分量都有一对儿get/set方法
var n=date.getXXX()获得分量的值
date.setXXX(n)设置分量的值,Day没有set
2.返回值:Date从1开始到31结束
除Date外,其余都是从0开始到 进制-1结束
强调:只有month需要修正,其余都正常
var birth = new Date("1993/10/18");
console.log(birth.getFullYear()); //1993
birth.setDate(17);
console.log(birth);
3.日期转字符串:
var str=date.toLocaleString(); //日期+时间
var str=date.toLocaleDateString(); //日期
var str=date.toLocaleTimeString(); //时间
var d = new Date;
console.log(d.toLocaleString());
console.log(d.toLocaleDateString());
console.log(d.toLocaleTimeString());
错误处理
错误对象:Error 封装错误信息的对象,在发生错误时自动创建
6种:
ReferenceError:找不到对象时
TypeError:错误的使用了类型或对象的方法时
RangeError:使用内置对象的方法时,参数超范围
SyntaxError:语法写错了
EvalError:错误的使用了Eval
URIError:URI错误
如何错误处理:
try{
可能发生错误的代码
}catch(err){
只有发生错误时才执行的代码——错误处理代码
}[finally{
无论是否出错,肯定都要执行的代码——善后工作(保存,释放
}]
try{
var now = oldnow;
document.write("成功");
}catch(err){
document.write("失败");
}finally{
document.write("释放");
}