Math和Date 学习总结
封面画师:唏嘘的星辰  p站ID:13312138
Math对象
Math对象API
向下求整
var n1 = Math.floor(4.9)console.log(n1)  // 4
向上求整
var n2 = Math.ceil(4.1)console.log(n2)  // 5
四舍五入
var n3 = Math.round(4.5)console.log(n3)  // 5
随机数 [0,1) 包前不包尾
var n4 = Math.random()
日期对象
创建日期对象:通过new Date()创建
new 创建一个新的, 申请内存空间的作用存储日期对象Date() 构造函数:用来创建对象的, 函数名首先字母是大写var now = new Date()console.log(now)
创建日期对象 Date带参数,参数是字符串时间,把参数转成对象格式
var before = new Date("2020-04-11 13:14:00")console.log(before)
创建日期字符串
var s1 = Date()console.log(s1)
对象与字符串
日期对象 例如now.getFullYear() 2020console.log(now.getFullYear()) 获取年的 2020console.log(s1.getFullYear()) s1.getFullYear is not a functionconsole.log(now.getMonth()+1) 获取月份之后加1console.log(now.getDate()) 获取日期console.log(now.getHours())  获取小时console.log(now.getMinutes()) 获取分钟console.log(now.getSeconds())  获取秒console.log(now.getDay()) 星期几,星期天是0console.log(now.getMilliseconds()) 获取ms数  1s=1000ms
Date是js内置对象
now() 获取当前时间距离1970年1月1日的ms数 时间戳var d1 = Date.now()console.log(d1)  1586582040
把日期字符串转成时间ms数
var d2 =  Date.parse("2020-04-11 15:32:30")console.log(d2)
如何把ms转成日期对象
把d1 = 1586582040转成年月日var d1 = 1586582040var n2 = new Date(d1)console.log(n2)console.log(n2.getFullYear()) //2020console.log(n2.getMonth()+1)
把日期对象转成字符串
console.log(n2.toDateString()) 只保留年月日字符串console.log(n2.toString()) 转成字符串(重要)console.log(n2.toTimeString())  只保留时间
年月日时分秒
getFullYear() 年getMonth()+1  月getDate()     日getHours()    时getMinutes()  分getSeconds()  秒getDay()      星期getMilliseconds() 毫秒
Date.now() 获取当前时间ms数Date.parse("2020-04-11 13:14:00") 获取当前时间ms数 
实战练习
5-20之间的随机数
// [5,20)之间的随机数
// 5-20   5-5 - 20-5  => 0-15=>
// Math.random()*15 [0-15)
// Math.floor(Math.random()*15) [0,14]
// [0,14] + 5   => [5,20)
var n5 =  Math.floor(Math.random()*15)+5  
console.log(n5)九九乘法表
// 1*1 = 1
// 2*1 = 2 2*2 = 4
// 3*1 =3 3*2=6 3*3 = 9
// 9*1=9                    9*9 = 81
for(var i = 1;i<10;i++){
    // i = 1-9 当成乘数
    for(var j = 1;j<=i;j++){
        // j = 当成被乘数
        // 如果i = 1 j= 1
        // 如果i=2   j = 1 2
        document.write(`${i}*${j}=${i*j}      `)
        // document.write(i+"*"+j+"="+i*j)
    }
    document.write("<br>")
}扩展了解
// 扩展了解
// Math.PI  π  90度 = Math.PI / 2  
// sin 正弦  sin90度 = 1  cos0度 = 1   sin0度= 0 cos90度=0 
// tan45度= 1  cot45度=1 
console.log(  Math.sin(90))  // 1 
console.log(Math.cos(Math.PI/4)) //  0.7
console.log(Math.sqrt(9)) // 3 Math.sqrt()开平方
console.log(Math.abs(-3)) // 3 绝对值
console.log(Math.pow(3,2)) // 9 第一个数的多少次方
console.log(Math.pow(3,5))//243
Math.floor()  
Math.ceil() 
Math.round() 
Math.random()
Math.pow(2,3) = 2^3
Math.pow(4,1/2) = 2 
Math.PI
Math.sqrt(9)  = 3
Math.abs(-3) = 3百钱买百鸡
/*
百钱买百鸡
公鸡 一个5块钱 x=0 x=0 x=0
母鸡 一个3块钱 y=0 y=0 y = 
小鸡 三个一块钱 z=0 z= 1 z =0
*/
//   公鸡的个数0-100
for(var x = 0;x<=100;x++){
// 母鸡的个数0-100
for(var j=0;j<=100;j++){ 
// 小鸡的个数0-100
for(var z = 0;z<=100;z++){
var c1 = (x+j+z==100)
var c2 = (5*x+3*j+z/3==100)
var c3 = (z%3==0)
if(c1&&c2&&c3){
  console.log(`公鸡的个数为:${x};母鸡的个数为:${j};小鸡的个数为:${z}`)
}
}
}
}
