一、Math對象的方法
1.求最值方法
①min()
語法:Math.min(num1,num2…numN)
功能:求一組數(shù)中的最小值。
返回值:Number。
②max()
語法:Math.max(num1,num2…numN)
功能:求一組數(shù)中的最大值。
返回值:Number。
<script> var min=Math.min(5,-4,0,9,108,-55); console.log(min);//-55 var min1=Math.min(5,-4,0,9,108,-55,"abc"); console.log(min1);//NaN var max=Math.max(88,0,6,85,199); console.log(ma);//199 </script>
2.取整方法
①ceil()
語法:Math.ceil(num)
功能:向上取整,即返回大于num的最小整數(shù)。
返回值:Number。
②floor
語法:Math.floor(num)
功能:向下取整,返回num的整數(shù)部分。
返回值:Number。
③round()
語法:Math.round (num)
功能:將數(shù)值四舍五入為最接近的整數(shù)。
返回值:Number。
var num=Math.ceil(189.99); console.log(num);//190 var num1=Math.ceil(189.09); console.log(num1);//190 var num2=189.09; var int1=Math.ceil(num2);//190 var int2=Math.floor(num2);//189 var int3=Math.round(num2);//189 var num3=189.69; var int3=Math.round(num3);//190
3.求絕對值
①abs()
語法:Math.abs (num)
功能:返回num的絕對值。
返回值:Number。
var nums=-55; console.log(Math.abs(nums));//55
4.生成隨機數(shù)
①random()
語法:Math.random()
功能:返回大于等于0小于1的一個隨機數(shù)。
返回值:Number。
說明:
求n到m之間的隨機整數(shù)的公式:
random=Math.floor(Math.random()*(m-n+1)+n);
var random=Math.random(); console.log(random);//每一次刷新都不一樣,小于1的隨機數(shù):0.458541256325412 //生成x~x之間的隨機整數(shù) function getRandom(n,m){ var choise=m-n+1;//隨機整數(shù)的個數(shù) return Math.floor(Math.random()*choise+n); } var random1=getRandom(2,6); console.log(random1);//5 3 2...
二、date對象
1.創(chuàng)建日期對象的方法
語法:new Date();
功能:創(chuàng)建一個日期時間對象
返回值:不傳參的情況下,返回當(dāng)前的日期時間對象。
說明:
如果想根據(jù)特定的日期和時間創(chuàng)建日期對象,必須傳入表示該日期的毫秒數(shù)或者是一組用逗號隔開的表示年月日時分秒的參數(shù)。
2.獲取日期時間的方法
1、getFullYear():返回4位數(shù)的年份
2、getMonth():返回日期中的月份,返回值為0-11
3、getDate():返回月份中的天數(shù)
4、getDay():返回星期,返回值為0-6
5、getHours():返回小時
6、getMinutes():返回分
7、getSeconds():返回秒
8、getTime():返回表示日期的毫秒數(shù)
<script> //創(chuàng)建一個日期時間對象 var weeks=["日","一","二","三","四","五","六"], today=new Date(); console.log(today);//Thu Jan 04 2018 15:43:49 GMT+0800 (中國標準時間) var today=new Date(), year=today.getFullYear(), month=today.getMonth()+1, date=today.getDate(), week=today.getDay(), hours=today.getHours(), minutes=today.getMinutes(), seconds=today.getSeconds(), times=today.getTime(), time=year+'年'+month+'月'+date+'日'+hours+'時' +minutes+'分'+seconds+'秒 星期'+weeks[week]; console.log("現(xiàn)在是:"+time); //現(xiàn)在是:2018年1月4日15時51分41秒 星期四 console.log(times);//從1970年1月1日00:00:00開始到現(xiàn)在時間的毫秒數(shù):1515052409017 </script>
3.設(shè)置日期時間的方法
1、setFullYear(year):設(shè)置4位數(shù)的年份
2、setMonth(mon):設(shè)置日期中的月份,從0開始,0表示1月
3、setDate():設(shè)置日期
4、setDay():設(shè)置星期,從0開始,0表示星期日
5、setHours():設(shè)置小時
6、setMinutes():設(shè)置分
7、setSeconds():設(shè)置秒
8、setTime():以毫秒數(shù)設(shè)置日期,會改變整個日期
//創(chuàng)建一個日期時間對象 var today=new Date(); today.setFullYear(2015); console.log(today.getFullYear());//2015 today.setMonth(8); console.log(today.getMonth());//8 today.setMonth(13); console.log(today.getMonth());//1
案例:50天之后是星期幾
<script> var today=new Date(); //第一種做法 //today.setDate(today.getDate()+50); //console.log(today.getDay()); //5 //第二種做法 var weeks=["日","一","二","三","四","五","六"]; var year=today.getFullYear(); var month=today.getMonth(); var day=today.getDate(); //創(chuàng)建一個目標日期對象 var temp = new Date(year,month,day+50); console.log("50天后的今天是:"+temp.getFullYear()+'-'+(temp.getMonth()+1)+'-'+temp.getDate() +'-'+'星期'+weeks[temp.getDay()]); //50天后的今天是:2018-2-23-星期五 </script>
推薦教程:《JS教程》