Math對象
Math常用API
生成隨機數(shù)? ? ? ? ? ? ? ? ?Math.random()
四舍五入? ? ? ? ? ? ? ? ? ? ?Math.round()
最大值? ? ? ? ? ? ? ? ? ? ? ? ?Math.max(多個數(shù)字)
最小值? ? ? ? ? ? ? ? ? ? ? ? ?Math.min()
絕對值? ? ? ? ? ? ? ? ? ? ? ? ?Math.abs()
向上取整? ? ? ? ? ? ? ? ? ? ??Math.ceil()
向下取證? ? ? ? ? ? ? ? ? ? ??Math.floor()
π? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??Math.PI
各進制間數(shù)字的轉換
tostring方法將十進制數(shù)字轉為其他進制
varx=110;
x.toString(2)//轉為2進制
x.toString(8)//轉為8進制
x.toString(16)//轉為16進制
parseInt方法將其他進制數(shù)字轉為十進制
varx="110"http://這是一個二進制的字符串表示
parseInt(x,2)//把這個字符串當做二進制, 轉為十進制
varx="70"http://這是一個八進制的字符串表示
parseInt(x,8)//把這個字符串當做八進制, 轉為十進制
varx="ff"http://這是一個十六進制的字符串表示
parseInt(x,16)//把這個字符串當做十六進制, 轉為十進制
parseInt方法是用于將字符串轉為數(shù)字的方法。 接受兩個參數(shù)。 第一個要轉換的字符串,第二個是可選的, 如果沒有值, 默認是10進制; 如果有值, 就是以該值為轉換進制
JSON格式
所有對象的屬性名必須是字符串,必須加引號
對象的最后一個元素后面不允許加逗號
js中對象:
varobj={
name:"張三",
}
json中的對象:
{
“name“:”張三”
}
例:
[
?? {
"name":"手機",
"price":"¥1999",
"color":"red"
?? },
?? {
"name":"電腦",
"price":"¥3998",
"color":"green"
?? }
]
Date對象
Date對象用來處理日期和時間
創(chuàng)建日期對象
vardate=newDate();//使用構造函數(shù)創(chuàng)建一個當前時間的對象
vardate=newDate("2017-03-22");//創(chuàng)建一個指定時間的日期對象
vardate=newDate("2017-03-22 00:52:34");//創(chuàng)建一個指定時間的日期對象
vardate=newDate(2017,2,22,0,52,34);
vardate=newDate(1523199394644);//參數(shù):毫秒值
結果:Tue Jul 30 2019 21:26:56 GMT+0800 (中國標準時間)
類型使用自 CTU(Coordinated Universal Time,國際協(xié)調時間)1970 年 1 月 1 日午夜(零時)開始經(jīng)過的毫秒數(shù)來保存日期。Date 類型保存的日期能夠精確到 1970 年 1 月 1 日之前或之后的 285616? 年。
Date構造函數(shù)當不傳遞任何參數(shù)的時候,返回的是當前時間。當傳入?yún)?shù)的時候,獲取的是傳進去的時間
獲取時間
使用日期對象的方法獲取日期的指定部分
getMilliseconds();//獲取毫秒值
getSeconds();//獲取秒
getMinutes();//獲取分鐘
getHours();//獲取小時
getDay();//獲取星期,0-6 ?? 0:星期天
getDate();//獲取日,即當月的第幾天
getMonth();//返回月份,注意從0開始計算,這個地方坑爹,0-11
getFullYear();//返回4位的年份? 如 2016
案例:將日期格式化成字符串
functionformatDate(date) {
vard=newDate(date),
month=''+(d.getMonth()+1),
day=''+d.getDate(),
year=d.getFullYear();
if(month.length<2)month='0'+month;
if(day.length<2)day='0'+day;
return[year,month,day].join('-');
? }
console.log(formatDate('Sun May 13,2016'));
案例:獲取當前時間 年月日時分秒 (刷新)
<divid="box"></div>
<script>
functionshowNowTime(){
vardate=newDate();
varyear=date.getFullYear();// 獲取年
varmonth=date.getMonth()+1;// 獲取月 日期對象中使用0~11來表示1~12月
vard=date.getDate();// 獲取日
varday=date.getDay();// 獲取星期幾
varhour=date.getHours();// 獲取到時
varminute=date.getMinutes();// 獲取分鐘
varsecond=date.getSeconds();// 獲取到秒數(shù)
// var msecond = date.getMilliseconds(); // 獲取到毫秒數(shù)
// console.log(month);
box.innerText=("現(xiàn)在是北京時間:"+year+"年"+month+"月"+d
+"日。星期"+day+"。"+hour+"時"+minute+"分"+second+"秒");
?? }
setInterval(showNowTime,1000);
</script>
日期轉為毫秒數(shù)(時間戳)
格林威治時間/格林尼治時間
Date.parse("2015-08-24")// 獲取1970年到設定時間的毫秒數(shù)
newDate().getTime()
+newDate();
案例:計算兩個日期的時間差值
functioncount(){
vardate1=newDate(2010,10,3);
vardate2=newDate(2017,9,24);
vardate=(date2.getTime()-date1.getTime())/(1000*60*60*24);/*不用考慮閏年否*/
alert("相差"+date+"天");
}
案例:日期差值;
vardate1=newDate('2013/04/02 18:00')
vardate2=newDate('2013/04/02 19:22:21')
vars1=date1.getTime(),s2=date2.getTime();
vartotal=(s2-s1)/1000;
varday=parseInt(total/(24*60*60));//計算整數(shù)天數(shù)
varafterDay=total-day*24*60*60;//取得算出天數(shù)后剩余的秒數(shù)
varhour=parseInt(afterDay/(60*60));//計算整數(shù)小時數(shù)
varafterHour=total-day*24*60*60-hour*60*60;//取得算出小時數(shù)后剩余的秒數(shù)
varmin=parseInt(afterHour/60);//計算整數(shù)分
varafterMin=total-day*24*60*60-hour*60*60-min*60;//取得算出分后剩余的秒數(shù)
案例:顯示中文時間
varchr=["零","一","二","三","四","五","六","七","八","九","十"];
init();
functioninit() {
vardiv=document.getElementsByTagName("div")[0];
setInterval(animation,16,div);
}
functionanimation(elem) {
vardate=newDate();
varyear=getChrYear(date.getFullYear());
varmonth=getNumberChr(date.getMonth()+1)+"月";
vardays=getNumberChr(date.getDate())+"日";
varweek="星期"+(date.getDay()===0?"日":getNumberChr(date.getDay()));
varhours=date.getHours();
hours=(hours<12?"上午 ":"下午 ")+getNumberChr(hours<12?hours:hours-12)+"點";
varminutes=getNumberChr(date.getMinutes(),true)+"分";
varseconds=getNumberChr(date.getSeconds(),true)+"秒";
elem.innerHTML=year+month+days+" "+week
+" "+hours+minutes+seconds;
}
functiongetChrYear(year) {
vara=Math.floor(year/1000);
varb=Math.floor(year/100)%10;
varc=Math.floor(year/10)%10;
vard=year%10;
if(a!==0)returnchr[a]+chr[b]+chr[c]+chr[d]+"年";
// ? ? ? ? ?? 三位
if(b!==0)returnchr[b]+chr[c]+chr[d]+"年";
// ? ? ? ? ? 兩位
if(c!==0)returnchr[c]+chr[d]+"年";
// ? ? ? ? ? 一位
returnchr[d]+"年";
}
functiongetNumberChr(num,zero) {
num=Math.floor(num);
if(num<10)return(zero?chr[0] :"")+chr[num];
if(num===10)returnchr[num];
if(num<20)return"十"+chr[num%10];
if(num%10===0&&num<100)returnchr[Math.floor(num/10)]+"十";
if(num<100)returnchr[Math.floor(num/10)]+"十"+chr[num%10];
if(num>100)return"輸入錯誤";
}
日期格式化
date.toLocalString();//本地風格的日期格式
date.toLocaleDateString();// 獲取日期
date.toLocaleTimeString();// 獲取時間
設置時間
date.setFullYear(2016)// 將日期對象中的年份設置為2016
date.setMonth(3)// 將日期對象中的月份設置為4月份
date.setDate(15)// 將日期對象中的日期設置為15號
date.setHours(12)// 將日期對象中的小時設置為12點
date.setMinutes(56)// 將日期對象中的分鐘設置為56分
date.setSeconds(23)// 將日期對象中的描述設置為23秒
date.setTime(0)// 將日期對象設置為0的時間戳
