JS知识整理随笔(5) Math和Date对象

Math

  • min  求一组书中的最小值  返回number 
  • max 求一组书中的最大值  返回number
  • ceil   向上取整
  • floor   返回整数部分
  • round   四舍五入
  • abs  绝对值 

Math.random

功能:返回大于等于0小于1的一个随机数

返回值number

说明

求n都m之间的随机数的公式

random=Math.floor(Math.random()*(m-n+1)+n);

 function getRandom(n,m){
       var chois=m-n+1;//随机整数的个数
       return Math.floor(Math.random()*chois);
   }
   var random1=getRandom(2,6);
   console.log(random1);

Date

如何创建一个日期对象

语法:new Date()

功能:创建一个日期时间对象

返回值:不传参的情况下,返回当前的日期时间对象

   var weeks=["日","一","二","三","四","五","六"]
        today=new Date(),
        year=today.getFullYear(),
        month=today.getMonth(),
        date=today.getDate(),
        week=today.getDay(),
        hours=today.getHours(),
        minutes=today.getMinutes(),
        seconds=today.getSeconds(),
        time=year+'年'+month+'月'+date+'日  '
        +'星期'+weeks[week];
        console.log(time)

var today=new Date();
 //50天之后是几号
 //1、
today.setDate(today.getDate()+50);
console.log(today.getDate());
2
var year=today.getFullYear();
var month=today.getMonth();
var day=today.getDate();
var temp=new Date(year,month,day+50);
var time="10天后的今天是: "+temp.getFullYear()+'-'
+(temp.getMonth()+1)+'-'+temp.getDate();
console.log(time)
原文地址:https://www.cnblogs.com/zry2510/p/7098617.html