Math方法
- abs
Math.abs(x) 返回一个数的绝对值。
console.log(Math.abs(-4))
- round
Math.round(x) 返回四舍五入后的整数。
console.log(Math.round(4.5))
console.log(Math.round(4.4))
- ceil
Math.ceil(x) 一个数向上取整后的值
console.log(Math.ceil(4.1));
- floor
Math.floor(x) 一个数向下取整后的值
console.log(Math.floor(4.1));
- max
Math.max() 返回零到多个数值中的最大值。
console.log(Math.max(4, 99, 5));
- min
Math.min() 返回零到多个数值中的最小值。
console.log(Math.min(4, 99, 6));
- random
Math.random() 返回一个 0 到 1 之间的伪随机数。
返回指定的两个数之间的随机数写法如下:
Math.random()*(终止-开始)+开始值
console.log(Math.ceil(Math.random() * 16 + 22));
- pow
Math.pow(x, y) 返回一个数的 y 次幂 即x的y次幂
console.log(Math.pow(2,3));
Date方法
- 日期的设置
日期的创建写法如下:
const date = new Date()
其中在Date()中若写入7个数字则分别代表 年份,月份,日期,小时,分钟,秒,毫秒
若写入6个数字则分别代表 年份,月份,日期,小时,分钟,秒
若写入5个数字则分别代表 年份,月份,日期,小时,分钟
若写入4个数字则分别代表 年份,月份,日期,小时
若写入3个数字则分别代表 年份,月份,日期
若写入2个数字则分别代表 年份,月份
注意 若写入1个数字 则代表毫秒 - getTime
getTime() 方法返回自 1970 年 1 月 1 日以来的毫秒数:
const time =new Date()
console.log(time.getTime());
- getYear
getYear()返回自1900年开始的年份 其返回值为两位 在遇到2000年时会发生错误
const time =new Date()
console.log(time.getYear())
- getFullYear
getFullYear() 方法以四位数字形式返回日期年份:解决getYear()的问题
const time =new Date()
console.log(time.getFullYear())
- getMonth
getMonth() 以数字(0-11)返回日期的月份:
const time =new Date()
console.log(time.getMonth()+1)
- getDate
getDate() 方法以数字(1-31)返回日期的日:
const time =new Date()
console.log(time.getDate())
- getHours
getHours() 方法以数字(0-23)返回日期的小时数:
const time =new Date()
console.log(time.getHours())
- getMinutes
getMinutes() 方法以数字(0-59)返回日期的分钟数:
const time =new Date()
console.log(time.getMinutes())
- getSeconds
getSeconds() 方法以数字(0-59)返回日期的秒数:
const time =new Date()
console.log(time.getSeconds())
- getMilliseconds
getMilliseconds() 方法以数字(0-999)返回日期的毫秒数
const time =new Date()
console.log(time.getMilliseconds())
- getDay
getDay() 方法以数字(0-6)返回日期的星期名(0代表这星期日):
const time =new Date()
console.log(time.getDay())
- setMonth
设置月(0-11) - setDate
以数值(1-31)设置日 - setFullYear()
设置年(可选月和日) - setHours()
设置小时(0-23) - setMinutes()
设置分(0-59) - setSeconds()
设置秒(0-59) - setMilliseconds()
设置毫秒(0-999) - setTime()
设置时间(从 1970 年 1 月 1 日至今的毫秒数)