08 Math和Date

98 阅读8分钟

Math

  • 处理数学问题,
  • 特点:不需要创建对象,直接类型名.方法Math.方法()

随机数random

  • Math.random() 这个方法是用来生成一个 0 ~ 1 之间的随机数
  • 每次执行生成的数字都不一样,但是一定是 0 ~ 1 之间的
  • 生成的数字包含 0 ,但是不包含 1
 var num = Math.random()
        console.log(num) // 得到一个随机数

取整方法round,ceil,floor

ceil
  • Math.ceil()是将一个小数向上取整得到的整数
var num = 10.1
        console.log(Math.ceil(num)) // 11

        var num2 = 10.9
        console.log(Math.ceil(num2)) // 11
floor
  • Math.floor()是将一个小数向下取整得到的整数
   var num = 10.1
        console.log(Math.floor(num)) // 10

        var num2 = 10.9
        console.log(Math.floor(num2)) // 10
round
  • Math.round()是将一个小数四舍五入变成一个整数
var num = 10.1
        console.log(Math.round(num)) // 10

        var num2 = 10.6
        console.log(Math.round(num2)) // 11

Math.round(),Math.ceil(),Math.floor()的区别

1.Math.round():根据“round”的字面意思“附近、周围”,可以猜测该函数是求一个附近的整数,看下面几个例子就明白。

小数点后第一位<5
正数:Math.round(11.46)=11
负数:Math.round(-11.46)=-11
 
小数点后第一位>5
正数:Math.round(11.68)=12
负数:Math.round(-11.68)=-12
 
小数点后第一位=5
正数:Math.round(11.5)=12
负数:Math.round(-11.5)=-11

总结:(小数点后第一位)大于五全部加,等于五正数加,小于五全不加。

2.Math.ceil():根据“ceil”的字面意思“天花板”去理解;

例如:
Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=12
 
Math.ceil(-11.46)=Math.ceil(-11.68)=Math.ceil(-11.5)=-11
 

3.Math.floor():根据“floor”的字面意思“地板”去理解;

例如:
Math.floor(11.46)=Math.floor(11.68)=Math.floor(11.5)=11
 
Math.floor(-11.46)=Math.floor(-11.68)=Math.floor(-11.5)=-12

绝对值abs

  • Math.abs() 是返回一个数字的绝对值
var num = -10
        console.log(math.abs(num)) // 10

最大值max

  • Math.max() 得到的是你传入的几个数字之中最大的那个数字
console.log(Math.max(1, 2, 3, 4, 5)) // 5

最小值min

  • Math.min() 得到的是你传入的几个数字之中最小的那个数字
 console.log(Math.min(5, 7, 1, 3, 9)) // 1

Math对象的方法

  • abs(x) 返回数的绝对值
  • acos(x)返回数的反余弦值
  • asin(x)返回数的反正弦值
  • atan(x)以介于-π/2与π/2弧度之间的数值来返回x的反正切值
  • atan2(y,x) 返回从x轴到点(x,y)的角度(介于-π/2与π/2弧度之间)
  • ceil(x)对数进行上舍入
  • cos(x) 返回数的余弦
  • exp(x) 返回e的指数
  • floor(x) 对数进行下舍入
  • log(x) 返回数的自然对数(底为e)
  • max(x,y) 返回x和y中的最大值
  • min(x,y) 返回x和y中的最小值
  • pow(x,y) 返回x的y次幂
  • random() 返回0~1之间的随机数
  • round(x) 把数四舍五入为最接近的整数
  • sin(x) 返回数的正弦
  • sqrt(x)返回数的平方根
  • tan(x) 返回角的正切
  • toSource() 返回该对象的源代码
  • valueOf() 返回Math对象的原始值

数字转换进制

进制

  • 0x表示十六进制,但是js会强制转换为十进制来运算,0xa == 10
  • 0开头表示八进制,同样强制转换为十进制来运算 010 == 8

进制.png

  • 1.十进制转其他进制 toString(进制)
  • 2.其他进制转十进制 parseInt(函数名,进制)

1.toString() 方法可以在数字转成字符串的时候给出一个进制

  • 语法: toString(你要转换的进制)
  var num = 100
        console.log(num.toString(2))   // 1100100
        console.log(num.toString(8))   // 144
        console.log(num.toString(16))  // 64

2.parseInt() 方法可以在字符串转成数字的时候把字符串当成多少进制转成十进制

  • 语法:parseInt(要转换的字符串,当做几进制来转换)
  var str = 100
        console.log(parseInt(str, 8)) // 64100当做一个 八进制 的数字转换成 十进制 以后得到的
        console.log(parseInt(str, 16)) // 256100当做一个 十六进制 的数字转换成 十进制 以后得到的console.log(num.toString(8)) 
        console.log(parseInt(str, 2)) // 4100当做一个 二进制 的数字转换成 十进制 以后得到的console.log(num.toString(16)) 

Date

js 提供的内置构造函数,专门用来获取时间的

创建日期时间对象new Date()

  • new Date()在不传参的情况下是默认返回当前时间
var time = new Date()
        console.log(time) // 当前时间 Sat Aug 27 2022 10:13:56 GMT+0800 (中国标准时间)
  • new Date()在传入参数的时候,可以获取到一个你传递进去的时间
 var time = new Date('2022-08-27 10:15:23')
        console.log(time) // 当前时间 Sat Aug 27 2022 10:15:23 GMT+0800 (中国标准时间)

英文日期

英文月份.png

英文星期几.png

new Date() 传递的参数有多种情况

new Date() 传递的参数的多种情况1.png

new Date() 传递的参数的多种情况2.png

常用方法

getFullYear
  • getFullYear 方式是得到指定字符串中的哪一年
var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getFullYear())  // 2019
getMonth
  • getMonth 方式是得到指定字符串中的哪一个月份
 var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getMonth())  // 3
  • 这里有一个地方要注意
  • 月份是从0开始的
  • 0表示1月,1表示2月,以此类推
// 计算机月份从 0 开始,正确月份需要 +1 
console.log('month:',(month+1))
getDate
  • getDate 方式是得到指定字符串中的哪一天
 var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getDate()) // 5
getHours
  • getHours 方式是得到指定字符串中的哪小时
var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getHours()) // 8
getMinutes
  • getMinutes 方式是得到指定字符串中的哪分钟
  var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getMinutes()) // 0
getSeconds
  • getSeconds 方式是得到指定字符串中的哪秒钟
var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getSeconds()) // 22
getDay
  • getDay 方式是得到指定字符串当前日期是一周中的第几天(周日是0,周六是6)
var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getDay()) // 5
getTime
  • getTime 方式是得到执行时间到 格林威治时间 的毫秒数
 var time = new Date(2019, 03, 05, 08, 00, 22, )
        console.log(time.getTime()) // 1554422422000
  • 1s = 1000ms

一、JavaScript获取当前年、月、日.

在JavaScript中,使用getFullYear()、getMonth()和getDate()这3种方法来获取当前的年、月、日。

JavaScript获取当前年、月、日

方法                 说明
getFullYear()    返回一个表示年份的4位数字
getMonth()     返回值是0(一月)到11(十二月)之间的一个整数
getDate()      返回值是1~31之间的一个整数

举例:
    <script type="text/javascript">
        var d = new Date();
        var my_day=d.getDate();
        var my_month=d.getMonth()+1;
        var my_year=d.getFullYear();
        document.write("今天是"+my_year+"年"+my_month+"月"+my_day+"日");
    </script>

二、 JavaScript方法获取当前时、分、秒

在JavaScript中,使用getHours()、getMinutes()、getSeconds()这3种方法分别用来获取当前的时、分、秒。
   <script type="text/javascript">
        var d = new Date();
        var my_hours=d.getHours();
        var my_minutes=d.getMinutes();
        var my_seconds=d.getSeconds();
        document.write("当前时间是:"+my_hours+":"+my_minutes+":"+my_seconds);
    </script>
 
日期时间格式化
Sat Aug 27 2022 10:15:23 GMT+0800 (中国标准时间)
实际显示时间格式
2022-08-27 10:15:23
2022/08/27 10:15:23
2022年8月27日  10  15  23 

时间格式化.png

工具函数
 /**************************************时间格式化处理************************************/
      function dateFtt(fmt, date) {
        var o = {
          "M+": date.getMonth() + 1, //月份
          "d+": date.getDate(), //日
          "h+": date.getHours(), //小时
          "m+": date.getMinutes(), //分
          "s+": date.getSeconds(), //秒
          "q+": Math.floor((date.getMonth() + 3) / 3), //季度
          S: date.getMilliseconds(), //毫秒
        };
        if (/(y+)/.test(fmt))
          fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + "").substr(4 - RegExp.$1.length)
          );
        for (var k in o)
          if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(
              RegExp.$1,
              RegExp.$1.length == 1
                ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length)
            );
        return fmt;
      }
 
      //创建时间格式化显示
      function crtTimeFtt() {
        var crtTime = new Date(); //当前时间
        return dateFtt("yyyy-MM-dd hh:mm:ss", crtTime); //直接调用公共JS里面的时间类处理的办法
      }
 
      console.log(crtTimeFtt()); //2018-05-27 17:56:20
 
momentjs.cn/

时间格式化2.png

计算时间差

  • 例如:我们现在计算一下 2019-01-01 00:00:00 到 2019-01-03 04:55:34 的时间差
  • 先获取两个时间点到 格林威治时间【计算机时间元点】(1970年1月1日(00:00:00 GMT)) 的毫秒数
  var time1 = new Date('2019-01-01 00:00:00')
        var time2 = new Date('2019-01-03 04:55:34')

        time1 = time1.getTime()
        time2 = time2.getTime()

        console.log(time1)
        console.log(time2)
  • 两个时间相减,得到两个时间点之间相差的毫秒数
 var differenceTime = time2 - time1
        console.log(differenceTime)  // 190534000
  • 毫秒转换成天
 var day = differenceTime / (1000 * 60 * 60 * 24) // 2.20525462962963
        day = Math.floor(day)
        console.log(day) // 2
  • 把我们计算的毫秒数换算成时间
  // 求你出生到现在经历多少天多少小时?
        var time1 = new Date('2022-8-25 10:50:50')
        var time2 = new Date('1992-11-30 11:20:45')
        time1 = time1.getTime()
        time2 = time2.getTime()

        //总毫秒数
        var time = time1 - time2
        console.log(time)

        //天数
        var day = time / (1000 * 60 * 60 * 24)
        day = Math.floor(day)
        console.log(day)

        //小时
        var afterHours = time - (1000 * 60 * 60 * 24 * day)
        var hours = afterHours / (1000 * 60 * 60)
        //  var hours =( time - (1000 * 60 * 60 * 24 * day)) / (1000 * 60 * 60)
        hours = Math.floor(hours)
        console.log(hours)

        //分钟
        var afterMinutes = afterHours - (1000 * 60 * 60 * hours)
        var minutes = afterMinutes / (1000 * 60)
        minutes = Math.floor(minutes)
        console.log(minutes)

        //秒
        var afterSeconds = afterMinutes - (1000 * 60 * minutes)
        var seconds = afterSeconds / (1000 * 60)
        seconds = Math.floor(seconds)
        console.log(seconds)

        //毫秒
        var milliSeconds = afterSeconds - (1000 * seconds)
        console.log(milliSeconds)

        document.write('1992-11-30 11:20:45 和 2022-8-25 10:50:50 之间相差了:')
        document.write(day + '天' + hours + '小时' + minutes + '分钟' + seconds + '秒' + milliSeconds + '毫秒')
 // 1992-11-30 11:20:45 和 2022-8-25 10:50:50 之间相差了:10859天23小时30分钟0秒5000毫秒