JS内置对象Math 和Data

1.Math

(1)Math.PI       表示常量  π

  console.log(Math.PI)                              //3.1415926535   

(2)Math.max()     最大值

        Math.min()      最小值

  console.log(Math.max(1,8,9,6,59,658,569,542))             //658
  console.log(Math.min(658,569,542,586,358,785,459,264))    //264

(4)Math.ceil ()       天花板,往上取整  

        Math.floor()        地板  往下取整

  console.log(Math.ceil(3.1))             //4
  console.log(Math.floor(3.7))            //3

(6)Math.round()   四舍五入

console.log(Math.round(3.1))        //3
  console.log(Math.round(3.7))    //4

(7)Math.random   随机数   [0~1)

[0~1) :

console.log(Math.random())       //0.8593806925764069

[0~3) :

console.log(Math.random())       //2.7221629645726204

[0~3]之间的整数

 console.log(Math.floor(Math.random()*4))  

(8)Math.abs   绝对值   

console.log(Math.abs(-3.1))        //3.1    
console.log(Math.abs(3.7))        //3.7

2.Data   日期和时间

(1)new Date()    创建当前时间
  var date=new Date()                         
  console.log(date) 

(2)格式化时间

 var date=new Date()                         
 console.log(date.toLocaleString())    //2019/9/29 下午8:35:13
var date=new Date()                         
  console.log(date.toLocaleDateString())     //2019/9/29
  var date=new Date()                         
  console.log(date.toLocaleTimeString())  

(3)获取具体的日期

 var date=new Date()                         
  console.log(date.getFullYear())     //2019
var date=new Date()                         
  console.log(date.getMonth()+1)     //
var date=new Date()                         
  console.log(date.getDay())    //0   星期日
 var date=new Date()                         
  console.log(date.getHours())    //20   
 var date=new Date()                         
  console.log(date.getMinutes())   //48
 var date=new Date()                         
  console.log(date.getSeconds())

(4)时间戳  从1970年到现在的时间 的总毫秒

 var date=+new Date()                         
  console.log(date) 

(5)倒计时

时间差:     T=parseInt((end时间戳 -start时间戳)/1000)

天:     day=parseInt(T/3600/24)

小时:  hour=parseInt(T/60/60)%24

分   :   minute=parseInt(T/60)%60

秒 :second=time%60

猜你喜欢

转载自www.cnblogs.com/zhaodz/p/11610145.html
今日推荐