JavaScript-内置对象

102 阅读1分钟

内置对象

Math 数组 字符串 document.write consolo.log...

Math

random ceil floor max min

Date 日期

  • Date()日期对象,是一个构造函数,使用时需要实例化后才能使用其中的具体方法和属性
  • Date实例是用来处理日期和时间的
  • 获取当前时间 var date = new Date() 获取指定时间 var date = new Date('2022/8/8')
 
        var arr = new Array()
        var obj = new Object()
        创建一个日期对象,不传递参数,返回当前系统的当前时间
        var date = new Date()
        
        //传递参数 常用的写法 数字型 2019,10,01  字符串型'2019-10-1 21:44'

        //格式化年月日
        var date = new Date()
        console.log(date.getFullYear());//返回当前日期的年2022
        console.log(date.getMonth() + 1);//返回的月份小一个月,记得+1
        console.log(date.getDate());//返回的是几号
        console.log(date.getDay());//返回的是星期几,周一到周六返回的是1~6,周日返回的是0
        //写一个2021年8月8日

        var year = date.getFullYear()
        var month = date.getMonth()+1
        var dates = date.getDate()
        var arr = [
            '星期日',
            '星期一',
            '星期二',
            '星期三',
            '星期四',
            '星期五',
            '星期六']
        var day = date.getDay()
        console.log("今天是" + year + "年" + month + "月" + dates + "日"+ " "+arr[day])

        //获取Date总的毫秒数,距离1970年1月1日过了多少毫秒数
        var date = new Date()
        console.log(date.valueOf())
        console.log(date.getTime())
        //简单写法
        var date1 = +new Date()
        //h5新增的
        console.log(Date.now())