Math方法以及Date方法总结

Math方法

  1. abs
    Math.abs(x) 返回一个数的绝对值。
console.log(Math.abs(-4))
//返回4
  1. round
    Math.round(x) 返回四舍五入后的整数。
console.log(Math.round(4.5))//返回5
console.log(Math.round(4.4))//返回4
  1. ceil
    Math.ceil(x) 一个数向上取整后的值
console.log(Math.ceil(4.1));
//返回5
  1. floor
    Math.floor(x) 一个数向下取整后的值
console.log(Math.floor(4.1));
//返回4
  1. max
    Math.max() 返回零到多个数值中的最大值。
console.log(Math.max(4, 99, 5));
//返回99
  1. min
    Math.min() 返回零到多个数值中的最小值。
console.log(Math.min(4, 99, 6));
//返回4
  1. random
    Math.random() 返回一个 0 到 1 之间的伪随机数。
    返回指定的两个数之间的随机数写法如下:
    Math.random()*(终止-开始)+开始值
console.log(Math.ceil(Math.random() * 16 + 22));
//返回22-38之间的随机数
  1. pow
    Math.pow(x, y) 返回一个数的 y 次幂 即x的y次幂
console.log(Math.pow(2,3));
//返回2的3次幂

Date方法

  1. 日期的设置
    日期的创建写法如下:
    const date = new Date()
    其中在Date()中若写入7个数字则分别代表 年份,月份,日期,小时,分钟,秒,毫秒
    若写入6个数字则分别代表 年份,月份,日期,小时,分钟,秒
    若写入5个数字则分别代表 年份,月份,日期,小时,分钟
    若写入4个数字则分别代表 年份,月份,日期,小时
    若写入3个数字则分别代表 年份,月份,日期
    若写入2个数字则分别代表 年份,月份
    注意 若写入1个数字 则代表毫秒
  2. getTime
    getTime() 方法返回自 1970 年 1 月 1 日以来的毫秒数:
const time =new Date()
console.log(time.getTime());
//返回自 1970 年 1 月 1 日以来的毫秒数
  1. getYear
    getYear()返回自1900年开始的年份 其返回值为两位 在遇到2000年时会发生错误
const time =new Date()
console.log(time.getYear())
//返回122 即千年虫问题导致
  1. getFullYear
    getFullYear() 方法以四位数字形式返回日期年份:解决getYear()的问题
const time =new Date()
console.log(time.getFullYear())
//返回2022即今年的年份
  1. getMonth
    getMonth() 以数字(0-11)返回日期的月份:
const time =new Date()
console.log(time.getMonth()+1)
//因为有+1的缘故所以返回了今天的月份8 若没有则返回7
  1. getDate
    getDate() 方法以数字(1-31)返回日期的日:
const time =new Date()
console.log(time.getDate())
//返回今天的日期 即1号
  1. getHours
    getHours() 方法以数字(0-23)返回日期的小时数:
const time =new Date()
console.log(time.getHours())
//返回现在的小时数
  1. getMinutes
    getMinutes() 方法以数字(0-59)返回日期的分钟数:
const time =new Date()
console.log(time.getMinutes())
//返回现在的分钟数
  1. getSeconds
    getSeconds() 方法以数字(0-59)返回日期的秒数:
const time =new Date()
console.log(time.getSeconds())
//返回此刻的秒数
  1. getMilliseconds
    getMilliseconds() 方法以数字(0-999)返回日期的毫秒数
const time =new Date()
console.log(time.getMilliseconds())
//返回此刻的毫秒数
  1. getDay
    getDay() 方法以数字(0-6)返回日期的星期名(0代表这星期日):
const time =new Date()
 console.log(time.getDay())
 //返回今天的星期
  1. setMonth
    设置月(0-11)
  2. setDate
    以数值(1-31)设置日
  3. setFullYear()
    设置年(可选月和日)
  4. setHours()
    设置小时(0-23)
  5. setMinutes()
    设置分(0-59)
  6. setSeconds()
    设置秒(0-59)
  7. setMilliseconds()
    设置毫秒(0-999)
  8. setTime()
    设置时间(从 1970 年 1 月 1 日至今的毫秒数)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值