數(shù)學(xué)對象
求最大值:Math.max(1,2,3,4,5);
求最小值:Math.min(1,2,3,4,5);
Math.ceil(15.66);向上取整//16
Math.floor(14.9);向下取整//14
Math.round(14.5) ;四舍五入//15
Math.random( );輸出0-1之間的隨機(jī)數(shù), 0 <= x < 1
封裝一個方法:隨機(jī)生成n到m的隨機(jī)數(shù)。
function random(n,m){
var num = m-n+1;
return Math.floor(Math.random()*num + n); }
隨機(jī)獲取字符串或數(shù)組下標(biāo)
例var arr = ["a","b","c","d"];
var index = Math.floor(Math.random()*arr.length);
Math.abs(number) 返回number的絕對值
Math.pow(number,power) 返回number的power次冪
Math.sqrt(number) 返回number的平方根
日期和時間
Date類型
使用UTC (Coordinated Universal Time,國際協(xié)調(diào)時間[又稱世界統(tǒng)一時間]) 1970年1月1日午夜(零時)開始經(jīng)過的毫秒來保存日期。在使用這種數(shù)據(jù)存儲格式的條件下,Date類型保存的日期能夠精確1970年1月1日之前或之后的285616年。
Date的創(chuàng)建
var time = new Date(); //創(chuàng)建一個日期對象
在調(diào)用Date構(gòu)造方法而不傳遞參數(shù)的情況下,新建的對象自動獲取當(dāng)前的時間和日期。
日期格式化方法
Date類型還有一些專門用于將日期格式化為字符串的方法。
toDateString() 以特定格式顯示星期幾、月、日和年
toTimeString() 以特定的格式顯示時、分、秒和時區(qū)
toLocaleDateString() 以特定地區(qū)格式顯示星期幾、月、日和年
toLocaleTimeString() 以特定地區(qū)格式顯示時、分、秒和時區(qū)
toUTCString() 以特定的格式顯示完整的UTC日期。
getTime() 獲取日期的毫秒數(shù),和valueOf()返回一致
setTime() 以毫秒數(shù)設(shè)置日期,會改變整個日期
getFullYear() 獲取四位年份
setFullYear() 設(shè)置四位年份,返回的是毫秒數(shù)
getMonth() 獲取月份,沒指定月份,從0開始算起
setMonth() 設(shè)置月份
getDate() 獲取日期
setDate() 設(shè)置日期,返回毫秒數(shù)
getDay() 返回星期幾,0表示星期日,6表示星期六
setDay() 設(shè)置星期幾,0表示星期日,6表示星期六
getHours() 返回時
setHours() 設(shè)置時
getMinutes() 返回分
setMinutes() 設(shè)置分
getSeconds() 返回秒
setSeconds() 設(shè)置秒
getMilliseconds() 返回毫秒數(shù)
setMilliseconds() 設(shè)置毫秒數(shù)