JavaScript学习笔记(八)内置对象Math、Date

JavaScript中对象分三种:自定义对象、内置对象、浏览器对象

内置对象是js语言自带的一些对象,供开发者使用,并提供了一些常用的或是最基本而必要的工能(属性和方法)。最大的优点是帮助快速开发

内置对象示例:Math、Date、Array、String

目录

一、查文档MDN/W3C

二、Math对象

2.1 Math是一个内置对象,它拥有一些数学常数属性和数学函数方法。

2.2 书写myMath对象

2.3 Math.random()

三、日期对象Date

3.1 创建对象

3.2 方法:

3.3 时间戳

 四、倒计时案例


一、查文档MDN/W3C

Mozilla开发者网络(MDN)提供了有关开放网络技术(Open Web)的信息,包括HTML、CSS和万维网及HTML5应用的API

MDN:http://developer.mozilla.org/zh-CN

二、Math对象

2.1 Math是一个内置对象,它拥有一些数学常数属性和数学函数方法。

Math不是一个构造函数,所以不需要new来调用,而是直接使用里面的属性和方法

Math.PI                  //圆周率

Math.floor()                //向下取整,往小了取

Math.ceil()                   //向上取整,往大了取

Math.round()                //四舍五入版,就进取整       注意-3.5结果是-3。.5特殊,往大了取

Math.abs()                        //绝对值

Math.max() / Math.min           //求最大和最小值

2.2 书写myMath对象

        var myMath = {
            PI : 3.1415926,
            max : function(){
                var max = arguments[0];
                for(i = 0;i < arguments.length;i++){
                    if(arguments[i]>max){
                        max = arguments[i];
                    }
                }
                return max;
            },
            min : function(){
                var min = arguments[0];
                for(i = 0;i < arguments.length;i++){
                    if(arguments[i]<min){
                        min = arguments[i];
                    }
                }
                return min;
            }
        }
        console.log(myMath.PI);
        console.log(myMath.max(9,101,99,100));
        console.log(myMath.min(9,101,99,100));

2.3 Math.random()

Math.random():随机数方法,返回一个随机的浮点小数 [0,1)

方法里不跟参数

① 返回包括两整数端点之间的随机数[min,max]

方法:Math.floor(Math.random()*(max-min+1))+min

        //返回包括端点两个数之间的随机整数
        function getRandom(min,max){
            return Math.floor(Math.random()*(max-min+1))+min;
        }
        console.log(getRandom(1,10));

三、日期对象Date

3.1 创建对象

用来处理日期时间,是一个构造函数,必须使用new来构造对象,实例化才能使用

        //Date对象,Date是一个构造函数,必须使用new来创建对象
        var arr = new Array();//创建一个数组对象
        var obj = new Object();//创建了一个对象实例
        var date = new Date();//
        //使用Date,如果没有参数,返回当前时间
        console.log(date);//Sat Apr 02 2022 16:40:26 GMT+0800 (中国标准时间)

参数写入时间,就返回参数里的时间,但是返回时间(****,**,**)有点特殊

        var date = new Date();
        console.log(date);//Sat Apr 02 2022 16:40:26 GMT+0800 (中国标准时间)
        var date1 = new Date(2022,4,2);
        console.log(date1);//Mon May 02 2022 00:00:00 GMT+0800 (中国标准时间)
        var date2 = new Date('2022-4-2');
        console.log(date2);//Sat Apr 02 2022 00:00:00 GMT+0800 (中国标准时间)

3.2 方法:

//格式化日期  时分秒

getHours()  //当前时

getMinutes() //分

getSeconds()//秒

getDay()  //获取星期几

getDate()  //获取当天日期

getMonth()  //获取当月(0-11)

getFullYear() //获取当年

3.3 时间戳

获取Date总的毫秒数,不是当前时间的毫秒数,而是距离距离1970年1月1日过了多少毫秒数。

1. 通过valueOf()       getTime()

2. 常用的简单写法+new Date()返回

        var date3 = +new Date();
        console.log('总的毫秒数'+date3);

3.HS新增的获得总的毫秒数Date.now()

        console.log(Date.now())

 四、倒计时案例

        function countDown(time){
            var nowTime = +new Date();
            var inputTime = +new Date(time);
            var times = (inputTime - nowTime)/1000;  //剩余时间总的秒数
            var d = parseInt(times / 60 / 60 / 24);
            d = d < 10 ? '0' + d : d;
            var h = parseInt(times / 60 / 60 % 24);
            h = h < 10 ? '0' + h : h;
            var m = parseInt(times / 60 % 60);
            m = m < 10 ? '0' + m : m;
            var s = parseInt(times % 60);
            s = s < 10 ? '0' + s : s;


            return d + '天' + h + '时' + m + '分' + s + '秒';
        }
        console.log(countDown('2022-4-6 13:00:00'));

猜你喜欢

转载自blog.csdn.net/weixin_44400887/article/details/123915758