1. 程式人生 > >JavaScript學習之小白篇(八-Math及Date用法)

JavaScript學習之小白篇(八-Math及Date用法)

好好學習 ,天天向上。Are you ready?在這裡插入圖片描述

一、Math
1. Math.PI 圓周率
2. 近似值
1> Math.round() 四捨五入
注: 負數 >0.5 進一 <= 0.5 捨去
2> Math.ceil() 向上取整
3> Math.floor() 向上取整
4> Math.abs() 絕對值
3. 最值
1> Math.max.apply(null,陣列) 最大值
2> Math.min.apply(null,陣列) 最小值
4. 隨機數
Math.floor(Math.random() * (max - min + 1) + min)
5. m的n次方
Math.pow(m,n)
6. 開方
Math.sqrt()
二、Date


1. 如何建立日期物件
new Date()
new Date(year,month,date,hours,minutes,seconds)
new Date(year,month,date)
2. 獲取日期時間
getFullYear()
getMonth()
getDate()
getDay()
getHours()
getMinutes()
getSeconds()
getMilliseconds()
getTime()
3. 設定日期時間
setFullYear()
setMonth()
setDate()
setHours()
setMinutes()
setSeconds()
setMilliseconds()
4. 本地格式
toLocaleString()
toLocaleDateString()
toLocaleTimeString()