美文网首页
JavaScript时间对象和Math对象

JavaScript时间对象和Math对象

作者: AuglyXu | 来源:发表于2018-11-09 14:32 被阅读0次

    日期对象

    • 一个案例解决
        function formartDate(date) {
            var arr = [];
            arr.push(date.getFullYear());//获取年份
            arr.push("-");
            arr.push(date.getMonth() + 1);//获取月份
            arr.push("-");
            arr.push(date.getDate());//获取日期
            arr.push(" ");
            arr.push(date.getHours());//获取小时
            arr.push(":");
            arr.push(date.getMinutes());//获取分钟
            arr.push(":");
            arr.push(date.getSeconds());//获取秒
            return arr.join("");
        }
    
    • 注意点: 月份为0~11月,所以获取的月份需要+1才是实际月份

    Math

    常用对象

    • Math.PI; 圆周率

    • Math.floor() 向下取整

    • Math.ceil() 向上取整

    • Math.round() 四舍五入

    • Math.abs() 绝对器

    • Math.random() 生成随机数

    相关文章

      网友评论

          本文标题:JavaScript时间对象和Math对象

          本文链接:https://www.haomeiwen.com/subject/tocoxqtx.html