老徐WEB:js入门学习 - javascript对象之Date对象

Date对象用于处理日期与时间。

通过new关键字创建Date对象实例,有四种方法。

var d = new Date() // 当前日期和时间
var d = new Date(milliseconds) //返回从 1970 年 1 月 1 日至今的毫秒数
var d = new Date(dateString)
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds)

Date对象的属性
constructor - 返回对创建此对象的 Date 函数的引用。
prototype - 使您有能力向对象添加属性和方法。


Date对象的方法


getDate() - 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
语法:dateObj.getDate()
返回值:1-31之间的一个整数

new Date('2020 05 03').getDate(); // 3
new Date('2020 05 22').getDate(); // 22


getDay() - 从 Date 对象返回一周中的某一天 (0 ~ 6)。
注意: 星期天为 0, 星期一为 1, 以此类推。
语法:dateObj.getDay()
返回值:0-6之间的一个整数

new Date('2020 05 22').getDay(); // 5
new Date('2020 05 23').getDay(); // 6


getFullYear() - 从 Date 对象返回四位数字的年份。
语法:dateObj.getFullYear()
返回值:四位数字的年份

new Date('2020 05 23').getFullYear(); // 2020

getHours() - 返回 Date 对象的小时 (0 ~ 23)。
语法:dateObj.getHours()
返回值:返回0-23之间的整数

new Date('2020 05 23 10:02:02').getHours(); // 10


getMilliseconds() - 返回 Date 对象的毫秒(0 ~ 999)。
语法:dateObj.getMilliseconds()
返回值:返回0-999之间的整数

new Date('2020 05 23 10:02:02:333').getMilliseconds(); // 333


getMinutes() - 返回 Date 对象的分钟 (0 ~ 59)。
语法:dateObj.getMinutes()
返回值:返回0-59之间的整数

new Date('2020 05 23 10:08:02:333').getMinutes(); // 8

getMonth() - 从 Date 对象返回月份 (0 ~ 11)。
注意: 一月为 0, 二月为 1, 以此类推。
语法:dateObj.getMonth()
返回值:0-11之间的整数

new Date('2020 05 23 10:08:02:333').getMonth(); // 4


getSeconds() - 返回 Date 对象的秒数 (0 ~ 59)。
语法:dateObj.getSeconds()
返回值:返回0-59之间的整数

new Date('2020 05 23 10:08:02:333').getSeconds(); // 2


getTime() - 返回 1970 年 1 月 1 日至今的毫秒数。
语法:dateObj.getTime()
返回值:整数

new Date('2020 05 23 10:08:02:333').getTime(); // 1590199682333


getTimezoneOffset() - 返回本地时间与格林威治标准时间 (GMT) 的分钟差。
语法:dateObj.getTimezoneOffset()
返回值:整数

new Date('2020 05 23 10:08:02:333').getTimezoneOffset(); // -480

 

getUTCDate() - 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。

getUTCDay() - 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
getUTCFullYear() - 根据世界时从 Date 对象返回四位数的年份。
getUTCHours() - 根据世界时返回 Date 对象的小时 (0 ~ 23)。
getUTCMilliseconds() - 根据世界时返回 Date 对象的毫秒(0 ~ 999)。
getUTCMinutes() - 根据世界时返回 Date 对象的分钟 (0 ~ 59)。
getUTCMonth() - 根据世界时从 Date 对象返回月份 (0 ~ 11)。
getUTCSeconds() - 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。

parse() - 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。

Date.parse('2020 05 23 10:03:23'); // 1590199403000
Date.parse('2020 05 23 10:03:25'); // 1590199405000

setDate() - 设置 Date 对象中月的某一天 (1 ~ 31)。
0 为上一个月的最后一天。
-1 为上一个月最后一天之前的一天。
如果当月有 31 天,32 为下个月的第一天。
如果当月有 30 天,32 为下一个月的第二天。
语法:dateObj.setDate(x)

var d = new Date();
d; // Sun Jun 07 2020 14:50:00 GMT+0800 (中国标准时间)
d.setDate(2); // 1591080600366
d; // Tue Jun 02 2020 14:50:00 GMT+0800 (中国标准时间)


setFullYear() - 设置 Date 对象中的年份(四位数字)。
语法:dateObj.setFullYear(year,month,day)
返回值:毫秒

var d = new Date();
d.setFullYear(2020,06,06); // 1594018522753
d; // Mon Jul 06 2020 14:55:22 GMT+0800 (中国标准时间)
d.setFullYear(2020,d.getMonth()-1,06); // 设置上个月的日期
d; // Sat Jun 06 2020 14:55:22 GMT+0800 (中国标准时间)


setHours() - 设置 Date 对象中的小时 (0 ~ 23)。
语法:dateObj.setHours(hour,min,sec,millisec)
返回值:毫秒

var d = new Date();
d; // Sun Jun 07 2020 15:10:15 GMT+0800 (中国标准时间)
d.setHours(15,20,30); // 1591514430032
d; // Sun Jun 07 2020 15:20:30 GMT+0800 (中国标准时间)


setMilliseconds() - 设置 Date 对象中的毫秒 (0 ~ 999)。
语法:dateObj.setMilliseconds(millisec)
返回值:毫秒

setMinutes() - 设置 Date 对象中的分钟 (0 ~ 59)。
语法:dateObj.setMinutes(min,sec,millisec)
返回值:毫秒

setMonth() - 设置 Date 对象中月份 (0 ~ 11)。
语法:dateObj.setMonth(month,day)
返回值:毫秒

setSeconds() - 设置 Date 对象中的秒钟 (0 ~ 59)。
语法:dateObj.setSeconds(sec,millisec)
返回值:毫秒

setTime() - setTime() 方法以毫秒设置 Date 对象。
语法:dateObj.setTime(millisec)
返回值:毫秒

var d = new Date();
d; // Sun Jun 07 2020 15:18:49 GMT+0800 (中国标准时间)
d.getTime(); // 1591514329289
d.setTime(1591514329289); // 
1591514329289
d; // Sun Jun 07 2020 15:18:49 GMT+0800 (中国标准时间)


setUTCDate() - 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
setUTCFullYear() - 根据世界时设置 Date 对象中的年份(四位数字)。
setUTCHours() - 根据世界时设置 Date 对象中的小时 (0 ~ 23)。
setUTCMilliseconds() - 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
setUTCMinutes() - 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
setUTCMonth() - 根据世界时设置 Date 对象中的月份 (0 ~ 11)。
setUTCSeconds() - setUTCSeconds() 方法用于根据世界时 (UTC) 设置指定时间的秒字段。
toDateString() - 把 Date 对象的日期部分转换为字符串。
语法:dateObj.toDateString()
返回值:字符串

var d = new Date();
d; // Tue Jun 09 2020 11:08:09 GMT+0800 (中国标准时间)
d.toDateString(); // Tue Jun 09 2020


toISOString() - 使用 ISO 标准返回字符串的日期格式。
toJSON() - 以 JSON 数据格式返回日期字符串。

toLocaleDateString() - 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
toLocaleTimeString() - 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
toLocaleString() - 据本地时间格式,把 Date 对象转换为字符串。
toString() - 把 Date 对象转换为字符串。
toTimeString() - 把 Date 对象的时间部分转换为字符串。
toUTCString() - 根据世界时,把 Date 对象转换为字符串。

UTC() - 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。
valueOf() - 返回 Date 对象的原始值。

勤学苦练,笨鸟先飞。关注【老徐WEB前端开发教程】公众号,听老徐说。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值