教程 > javascript 教程 > 阅读:59

javascript date 对象——迹忆客-ag捕鱼王app官网

date 对象是一种内置于 javascript 语言的数据类型。 日期对象是使用 new date( ) 创建的,如下所示。

new date( )
new date(milliseconds)
new date(datestring)
new date(year,month,date[,hour,minute,second,millisecond ])

创建 date 对象后,可以使用多种方法对其进行操作。 大多数方法只允许您使用本地时间或 utc(通用或 gmt)时间获取和设置对象的年、月、日、小时、分钟、秒和毫秒字段。

date 对象属性

序号 属性 描述
1 constructor 返回对创建此对象的 date 函数的引用。
2 propertype 使您有能力向对象添加属性和方法。

date 对象方法

序号 方法 描述
1 getdate() 从 date 对象返回一个月中的某一天 (1 ~ 31)。
2 getday() 从 date 对象返回一周中的某一天 (0 ~ 6)。
3 getfullyear() 从 date 对象以四位数字返回年份。
4 gethours() 返回 date 对象的小时 (0 ~ 23)。
5 getmilliseconds() 返回 date 对象的毫秒(0 ~ 999)。
6 getminutes() 返回 date 对象的分钟 (0 ~ 59)。
7 getmonth() 从 date 对象返回月份 (0 ~ 11)。
8 getseconds() 返回 date 对象的秒数 (0 ~ 59)。
9 gettime() 返回 1970 年 1 月 1 日至今的毫秒数。
10 gettimezoneoffset() 返回本地时间与格林威治标准时间 (gmt) 的分钟差。
11 getutcdate() 根据世界时从 date 对象返回月中的一天 (1 ~ 31)。
12 getutcday() 根据世界时从 date 对象返回周中的一天 (0 ~ 6)。
13 getutcfullyear() 根据世界时从 date 对象返回四位数的年份。
14 getutchours() 根据世界时返回 date 对象的小时 (0 ~ 23)。
15 getutcmilliseconds() 根据世界时返回 date 对象的毫秒(0 ~ 999)。
16 getutcminutes() 根据世界时返回 date 对象的分钟 (0 ~ 59)。
17 getutcmonth() 根据世界时从 date 对象返回月份 (0 ~ 11)。
18 getutcseconds() 根据世界时返回 date 对象的秒钟 (0 ~ 59)。
19 getyear() 已废弃。 请使用 getfullyear() 方法代替。
20 parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
21 setdate() 设置 date 对象中月的某一天 (1 ~ 31)。
22 setfullyear() 设置 date 对象中的年份(四位数字)。
23 sethours() 设置 date 对象中的小时 (0 ~ 23)。
24 setmilliseconds() 设置 date 对象中的毫秒 (0 ~ 999)。
25 setminutes() 设置 date 对象中的分钟 (0 ~ 59)。
26 setmonth() 设置 date 对象中月份 (0 ~ 11)。
27 setseconds() 设置 date 对象中的秒钟 (0 ~ 59)。
28 settime() settime() 方法以毫秒设置 date 对象。
29 setutcdate() 根据世界时设置 date 对象中月份的一天 (1 ~ 31)。
30 setutcfullyear() 根据世界时设置 date 对象中的年份(四位数字)。
31 setutchours() 根据世界时设置 date 对象中的小时 (0 ~ 23)。
32 setutcmilliseconds() 根据世界时设置 date 对象中的毫秒 (0 ~ 999)。
33 setutcminutes() 根据世界时设置 date 对象中的分钟 (0 ~ 59)。
34 setutcmonth() 根据世界时设置 date 对象中的月份 (0 ~ 11)。
35 setutcseconds() setutcseconds() 方法用于根据世界时 (utc) 设置指定时间的秒字段。
36 setyear() 已废弃。请使用 setfullyear() 方法代替。
37 todatestring() 把 date 对象的日期部分转换为字符串。
38 togmtstring() 已废弃。请使用 toutcstring() 方法代替。
39 toisostring() 使用 iso 标准返回字符串的日期格式。
40 tojson() 以 json 数据格式返回日期字符串。
41 tolocaledatestring() 根据本地时间格式,把 date 对象的日期部分转换为字符串。
42 tolocaletimestring() 根据本地时间格式,把 date 对象的时间部分转换为字符串。
43 tolocalestring() 根据本地时间格式,把 date 对象转换为字符串。
44 tostring() 把 date 对象转换为字符串。
45 totimestring() 把 date 对象的时间部分转换为字符串。
46 toutcstring() 根据世界时,把 date 对象转换为字符串。
47 valueof() 返回 date 对象的原始值。

date 静态方法

除了前面列出的许多方法之外,date 对象还定义了两个静态方法。 这些方法是通过 date() 构造函数本身调用的。

序号 方法 描述
1 date.parse() 解析日期和时间的字符串表示形式,并返回该日期的内部毫秒表示形式。
2 date.utc() 返回指定 utc 日期和时间的毫秒表示。

查看笔记

扫码一下
查看教程更方便
网站地图