Math对象与Date对象的方法

Math对象的方法

abs

返回绝对值

console.log(Math.abs(-4))
//4

max

返回最大值

console.log(Math.max(1,2,3,4,5));
//5

min

返回最小值

console.log(Math.min(1,2,3,4,5));
//1

pow

返回幂匀速后的值

		console.log(Math.pow(2,3))
		console.log(2**3)
		// 8 与**相同

round

返回四舍五入后的值

console.log(Math.round(5.4))
//5
console.log(Math.round(5.5))
//6

ceil

返回向上取整后的值

console.log(Math.ceil(5.5))
//6

floor

返回向下取整后的值

console.log(Math.floor(5.5))
//5

random

生产0-1之间的随机数

			console.log(Math.ceil((Math.random()*22)+16));
		// Math.random()*开始值与结束值的插值 + 开始值
		// 生成开始值与结束值之间的随机数

Date对象的方法

使用Date对象前需构建一个Date对象

let time = new Date();

设置时间

		const time = new Date(2022, 8, 10 , 12, 12, 0, 0)
		//设置时间位2022年7月10日12时12分0秒0毫秒
		// 只设置2022时会设置为毫秒
		let time = new Date()
		time.setFullYear(2022)
		time.setMinutes(8)
		time.setDate(1)

可在构建时设置时间,也可在构建后通过.setFullYear,.setMinutes,.setDate等set方法设置时间

不设置时间默认使用当前时间

getFullYear

返回指定日期的年份(1000 年到 9999 年之间的日期的四位数字)。

		let time = new Date()
		console.log(time.getFullYear()); 

getYear

返回1900年开始计算的年份

		let time = new Date()
		console.log(time.getYear()); 

getMonth

返回指定日期的月份(从 0 到 11)

		let time = new Date()
		console.log(time.getMonth()); 

getDate

返回指定日期的日(从 1 到 31)

		let time = new Date()
		console.log(time.getDate()); 

getDay

返回指定日期的周几(从 0 到 6)

		let time = new Date()
		console.log(time.getDay()); 

getHours

返回小时(从 0-23)

		let time = new Date()
		console.log(time.getHours()); 

getMinutes

返回分钟(从 0-59)

		let time = new Date()
		console.log(time.getMinutes()); 

getSeconds

返回秒数(从 0-59)

		let time = new Date()
		console.log(time.getSeconds()); 

getTime

返回自 1970 年 1 月 1 日开始至指定日期的毫秒数。

		let time = new Date()
		console.log(time.getTime()); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值