一、方法:1.1 常用方法:,我来为大家讲解一下关于javascript 类定义?跟着小编一起来看一看吧!

javascript 类定义(javascript内置对象总结-)

javascript 类定义

一、方法:

1.1 常用方法:

1.1.1 Date.UTC - 方法接受的参数同日期构造函数接受最多参数时一样,返回从1970-1-1 00:00:00 UTC到指定日期的的毫秒数

语法:

Date.UTC(year,month[,date[,hrs[,min[,sec[,ms]]]]])

案例:

const utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5)); const utcDate2 = new Date(Date.UTC(0, 0, 0, 0, 0, 0)); console.log(utcDate1.toUTCString()) // 输出:Fri, 02 Feb 1996 03:04:05 GMT console.log(utcDate2.toUTCString()) // 输出:Sun, 31 Dec 1899 00:00:00 GMT

1.1.2 Date.parse - 方法解析一个表示某个日期的字符串,并返回从1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的UTC时间)的毫秒数,如果该字符串无法识别,或者一些情况下,包含了不合法的日期数值(如:2015-02-31),则返回值为NaN

语法:

Date.parse(dateString) // 隐式调用 new Date(dateString).getTime()

案例:

const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT'); const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT'); console.log(unixTimeZero) // 输出:0 console.log(javaScriptRelease) // 输出:818035920000

1.1.3 Date.prototype.getDay() - 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天

语法:

dateObj.getDay()

案例:

const birthday = new Date(); const day1 = birthday.getDay() console.log(day1) // 输出:4

1.1.4 Date.prototype.getFullYear() - 方法根据本地时间返回指定日期的年份

语法:

dateObj.getFullYear()

案例:

const moonLanding = new Date(); console.log(moonLanding.getFullYear()) // 输出:2021

1.1.5 Date.prototype.getMonth() - 根据本地时间,返回一个指定的日期对象的月份,为基于0的值(0表示一年中的第一月)

语法:

dateObj.getMonth()

案例:

const moonLanding = new Date(); console.log(moonLanding.getMonth()); // 输出:1,显示月份时要加1

1.1.6 Date.prototype.getDate() - 根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从1--31)

语法:

dateObj.getDate()

案例:

const birthday = new Date(); const date1 = birthday.getDate(); console.log(date1) // 输出:25

1.1.7 Date.prototype.getHours() - 方法根据本地时间,返回一个指定的日期对象的小时

语法:

dateObj.getHours()

案例:

const birthday = new Date(); console.log(birthday.getHours()) // 输出:14

1.1.8 Date.prototype.getMinutes() - 方法根据本地时间,返回一个指定的日期对象的分钟数

语法:

dateObj.getMinutes()

案例:

const birthday = new Date(); console.log(birthday.getMinutes()) // 输出:33

1.1.9 Date.prototype.getSeconds() - 方法根据本地时间,返回一个指定的日期对象的秒数

语法:

dateObj.getSeconds()

案例:

const moonLanding = new Date(); console.log(moonLanding.getSeconds()) // 输出:18

1.1.10 Date.prototype.getTime() - 方法返回一个时间的格林威治时间数值

语法:

dateObj.getTime()

案例:

const moonLanding = new Date(); console.log(moonLanding.getTime()) // 输出:-14254782000

1.1.11 Date.prototype.setDate() - 方法根据本地时间来指定一个日期对象的天数

语法:

dateObj.setDate(dayValue)

案例:

const event = new Date() // 现在是2021-02-25 event.setDate(22); console.log(event.getDate()) // 输出:22 event.setDate(33) // 当前月只有28天,所以超出的天数是下个月的 console.log(event.getDate()) // 输出:5

1.1.12 Date.prototype.setMonth() - 方法根据本地时间为一个设置年份的日期对象设置月份

语法:

dateObj.setMonth(monthValue[, dayValue])

案例:

const event = new Date(); event.setMonth(13); // 当前年只有12个月,所以超出的月数是下一年的月份 console.log(event.getMonth()) // 输出:1 console.log(event) // 输出:Fri Feb 25 2022 14:44:52 GMT 0800 (中国标准时间)

1.1.13 Date.prototype.toString()和Date.prototype.toDateString() - 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串

语法:

dateObj.toDateString()

案例:

const event = new Date(); console.log(event.toString()) // 输出:Thu Feb 25 2021 14:48:39 GMT 0800 (中国标准时间) console.log(event.toDateString()) // 输出:Thu Feb 25 2021

1.1.14 Date.prototype.toString() - 方法

语法:

dateObj.toJSON()

案例:

const event = new Date(); const jsonDate = event.toJSON(); console.log(jsonDate) // 输出:2021-02-25T06:51:28.378Z console.log(new Date(jsonDate).toUTCString()) // 输出:Thu, 25 Feb 2021 06:51:28 GMT

,