JavaScript内置对象→Math,Date

Math对象(数学对象JavaScript内置对象,无需创建,直接存在

        1.Math对象下面的属性和方法
        Math.PI : 表示π
        console.log(Math.PI);
        Math.pow() : 求幂
        console.log(Math.pow(2, 4));
        Math.random() : 随机数
        console.log(Math.random());
        Math.ceil() : 向上取整
        console.log(Math.ceil(2.000009));
        Math.floor() :向下取整
        console.log(Math.floor(2.9999999));
        Math.round() : 四舍五入取整
        console.log(Math.round(3.56));
        Math.abs() : 绝对值
        console.log(Math.abs(-100));
        Math.sqrt() : 开根号
        console.log(Math.sqrt(4));
        Math.max() : 求最大值
        console.log(Math.max(23, 567, 23, 1234, 567));
        Math.min() : 求最小值
        console.log(Math.min(23, 567, 23, 1234, 567));

 Date对象(日期对象)

1.创建日期对象-构造函数或者类

var d=new Date --d:为实例对象, Date:为构造函数或类、

日期对象的方法:

        var d = new Date();
     getYear() 获取当前离1900年的年份差
     getFullYear() 获取年份
     getMonth()  获取月份0-11表示  1月-12月
     getDate() 获取日期
     getDay()  获取星期 0-6表示星期日-星期六
     getHours() 获取时
     getMinutes() 获取分
     getSeconds() 获取秒
     getTime() 获取1970.1.1离当前的时间的毫秒数。(unix系统)

三种设置日期方法 

1.字符串设置-(值不会自动进位)

        var date = new Date('2020-10-1 12:12:12');
        // var date = new Date('2020/10/1 12:12:12');
        // var date = new Date('2020.10.1  12:12:12');
        // var date = new Date('2020\10\1 12:12:12'); //不成立   \斜杠代表转义字符。 例如:\n换行
        console.log(date.toLocaleString()); //2020/10/1 下午12:12:12

2.数字设置-(值会自动进位)

    var date = new Date(2020, 9, 1, 12, 13, 14);
        console.log(date.toLocaleString());//2020/10/1 下午12:13:14 设置月份自动+1

        var date = new Date(2020, 13, 1, 12, 13, 14);
        console.log(date.toLocaleString()); //2021/2/1 下午12:13:14

3.设置日期 - 通过日期对象下面的方法。--自动进位(get - > set)

        var date = new Date();
        date.setDate(date.getDate() + 10); //获取今天的日期+10天,赋值回去
        console.log(date.toLocaleString()); //2020/7/9 下午3:37:30

        date.setMonth(date.getMonth() + 10); //设置月份,不需要获+1,设置的时候自动抵消。
        console.log(date.toLocaleString()); //2021/5/29 下午3:39:02

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值