持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第15天,点击查看活动详情
前面值类型和引用类型那篇文章我们简单地介绍过日期对象,这篇文章是详细篇。
构造函数
date对象的构建函数可以创建一个Date实例,表示某个时刻。
1. 语法
new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
2. 参数说明
- 没有参数: 表示实例化时刻的日期和时间
- Unix时间戳:
- value: 时间戳是一个整数,表示自1970年1月1日 00:00:00 UTC以来的毫秒数,忽略了闰秒。
- 时间戳字符串: 表示日期的字符串值。
- 分别提供日期与时间的每一个成员:至少有年份和月份。
- year: 表示年份的整数值
- monthIndex: 表示月份的整数值,从0(1月)到11(12月)
- date: 可选。表示一个月中的第几天的整数值
- hours: 可选。表示一天中的小时数的整数值(24小时制)。默认值为0。
- minutes: 可选。表示一个完整时间中的分钟部分的整数值,默认值是0.
- seconds: 可选。表示一个完整时间中的秒部分的整数值。默认值是0.
- milliseconds: 可选。表示一个完整时间的毫秒部分的整数值,默认值是0.
属性
1. Date.prototype
允许为Date对象添加属性
2. Date.length
Date.length的值是7。这是该构造函数可接受的参数个数。
方法
1. Date.now()
Date.now() 方法返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。
1.1 语法
let timeInMs = Date.now();
1.2 应用
// 当前时间的秒数
let A = Date.now();
console.log(A); // 1666579361062
// 获取当前日期
let date = Date(Date.now());
console.log(date); // Mon Oct 24 2022 10:44:32 GMT+0800 (中国标准时间)
// 相当于
let dateVal = new Date();
console.log(dateVal); // Mon Oct 24 2022 10:44:32 GMT+0800 (中国标准时间)
2. Date.parse()
Date.parse() 方法解析一个表示某个日期的字符串,并返回从 1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的 UTC 时间)的毫秒数,如果该字符串无法识别,或者一些情况下,包含了不合法的日期数值(如:2015-02-31),则返回值为 NaN。
2.1 语法
显示调用
Date.parse(dateString)
隐式调用
new Date(dateString).getTime()
2.2 参数说明
- dateString: 一个符合日期格式的字符串。例如"Dec 25, 1995", "Mon, 25 Dec 1995 13:30:00 GMT","2011-10-10"
2.3 用法
let date = Date.parse('2022-12-04');
console.log(date); // 1670112000000
3. Date.UTC()
Date.UTC() 方法接受的参数同 Date 构造函数接受最多参数时一样,但该前者会视它们为 UTC 时间,其返回从 1970 年 1 月 1 日 00:00:00 UTC 到指定时间的毫秒数。
3.1 语法
Date.UTC(year)
Date.UTC(year, month)
Date.UTC(year, month, day)
Date.UTC(year, month, day, hour)
Date.UTC(year, month, day, hour, minute)
Date.UTC(year, month, day, hour, minute, second)
Date.UTC(year, month, day, hour, minute, second, millisecond)
3.2 参数说明
- year:一个表示年份的整数值。从 0 到 99 的值会被映射到 1900 至 1999 年。其它的值则代表实际的年份。
- month: 可选。0(一月)到 11(十二月)之间的一个整数,表示月份。如果忽略该值,则默认为 0。
- date: 可选。1 到 31 之间的一个整数,表示某月当中的第几天。如果忽略该值,则默认为 1。
- hour: 可选。0 到 23 之间的一个整数,表示小时。如果忽略该值,则默认为 0。
- minute: 可选。0 到 59 之间的一个整数,表示分钟。如果忽略该值,则默认为 0。
- second: 可选。0 到 59 之间的一个整数,表示秒。如果忽略该值,则默认为 0。
- millisencond: 可选。0 到 999 之间的一个整数,表示毫秒。如果忽略该值,则默认为 0。
3.3 UTC时间
- UTC时间是协调世界时,也就是说世界标准时间。而Date构造函数的时间是本地时间。
- Date.UTC()返回一个时间数值,而不是一个Date对象。
3.4 用法
let date = Date.UTC(2018, 11, 1, 0, 0, 0);
console.log(date); // 1543622400000
const utcDate = new Date(Date.UTC(2018, 11, 1, 0, 0, 0));
console.log(utcDate); // Sat Dec 01 2018 08:00:00 GMT+0800 (中国标准时间)
实例属性
1. Date.prototype.constructor
返回创建了实例的构造函数,默认是 Date 构造函数。
实例方法
1. Date.prototype.getDate()
根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从 1--31)。
1.1 语法
dateObj.getDate()
1.2 用法
let Xmas95 = new Date("December 25, 1995 23:15:00");
let day = Xmas95.getDate();
alert(day); // 25
2. Date.prototype.getDay()
getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天。
2.1 语法
dateObj.getDay()
2.2 用法
let Xmas95 = new Date("December 25, 1995 23:15:30");
let weekday = Xmas95.getDay();
console.log(weekday); // 1
3. Date.prototype.getFullYear()
getFullYear() 方法根据本地时间返回指定日期的年份。
3.1 语法
dateObj.getFullYear();
3.2 用法
let today = new Date();
let year = today.getFullYear();
console.log(year); // 2022
4. Date.prototype.getMonth()
根据本地时间,返回一个指定的日期对象的月份,为基于 0 的值(0 表示一年中的第一月)。
4.1 语法
dateObj.getMonth()
4.2 用法
let Xmas95 = new Date('December 25, 1995 23:15:30');
let month = Xmas95.getMonth();
console.log(month); // 11
5. Date.prototype.getHours()
getHours() 方法根据本地时间,返回一个指定的日期对象的小时。
5.1 语法
dateObj.getHours()
5.2 用法
let Xmas95 = new Date("December 25, 1995 23:15:00");
let hours = Xmas95.getHours();
alert(hours); // 23
6. Date.prototype.getMinutes()
getMinutes() 方法根据本地时间,返回一个指定的日期对象的分钟数。
6.1 语法
dateObj.getMinutes()
6.2 用法
let Xmas95 = new Date("December 25, 1995 23:15:00");
let minutes = Xmas95.getMinutes();
console.log(minutes); // 15
7. Date.prototype.getSeconds()
getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数。
7.1 语法
dateObj.getSeconds()
7.2 用法
let Xmas95 = new Date("December 25, 1995 23:15:30");
let secs = Xmas95.getSeconds();
console.log(secs); // 30
8. Date.prototype.getMilliseconds()
getMilliseconds() 方法,根据本地时间,返回一个指定的日期对象的毫秒数。
8.1 语法
dateObj.getMilliseconds()
8.2 用法
let ms;
Today = new Date();
ms = Today.getMilliseconds();
console.log(ms); // 164
9. Date.prototype.getTime()
getTime() 方法返回一个时间的格林威治时间数值。你可以使用这个方法把一个日期时间赋值给另一个Date 对象。
9.1 语法
dateObj.getTime()
9.2 用法
9.2.1 复制日期对象
let birthday = new Date(1991, 9, 17);
let copy = new Date();
copy.setTime(birthday.getTime());
测量代码执行时间
let end, start, i;
start = new Date();
for (i = 0; i < 1000; i++) {
Math.sqrt(i);
}
end = new Date();
console.log("Operation took " + (end.getTime() - start.getTime()) + " msec");
10. 获取UTC时间的方法
获取UTC时间的方法其实和本地时间的用法是一样的,只是方法名不同而已,具体语法可以参考上面本地时间。
- Date.prototype.getTimezoneOffset(): 返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟。
- Date.prototype.getUTCDate():以世界时为标准,返回一个指定的日期对象为一个月中的第几天
- Date.prototype.getUTCDay():以世界时为标准,返回一个指定的日期对象为一星期中的第几天,其中 0 代表星期天。
- Date.prototype.getUTCFullYear():以世界时为标准,返回一个指定的日期对象的年份。
- Date.prototype.getUTCHours():以世界时为标准,返回一个指定的日期对象的小时数。
- Date.prototype.getUTCMilliseconds():以世界时为标准,返回一个指定的日期对象的毫秒数。
- Date.prototype.getUTCMinutes():以世界时为标准,返回一个指定的日期对象的分钟数。
- Date.prototype.getUTCMonth():以世界时为标准,返回一个指定的日期对象的月份,它是从 0 开始计数的(0 代表一年的第一个月)。
- Date.prototype.getUTCSeconds():以世界时为标准,返回一个指定的日期对象的秒数。
11. Date.prototype.setDate()
从这个方法开始是根据本地时间指定日期相关参数的
setDate() 方法根据本地时间来指定一个日期对象的天数。
11.1 语法
dateObj.setDate(dayValue)
11.2 参数说明
- dayValue:整数,表示该月的第几天。
如果是0,就设置成上个月的最后一天。如果是负值,日期会设置为上个月最后一天往前数这个负数绝对值天数后的日期。-1 会设置为上月最后一天的前一天。
11.3 用法
let theBigDay = new Date(1962, 6, 7); // 1962-07-07
theBigDay.setDate(24); // 1962-07-24
theBigDay.setDate(32); // 1962-08-01
12.Date.prototype.setFullYear()
setFullYear() 方法根据本地时间为一个日期对象设置年份。
12.1 语法
dateObj.setFullYear(yearValue[, monthValue[, dayValue]])
12.2 参数说明
- yearValue:指定年份的整数值,例如 1995。
- monthValue:一个 0 到 11 之间的整数值,表示从一月到十二月。
- dayValue:一个 1 到 31 之间的整数值,表示月份中的第几天。如果你指定了 dayValue 参数,就必须同时指定 monthValue。
如果没有指定 monthValue 和dayValue 参数,将会使用 getMonth 和getDate 方法的返回值。
如果有一个参数超出了合理的范围,setFullYear 方法会更新其他参数值,日期对象的日期值也会被相应更新。例如,为 monthValue 指定 15,则年份会加 1,月份值会为 3。
12.3 用法
let theBigDay = new Date();
theBigDay.setFullYear(1997);
13. Date.prototype.setMonth()
setMonth() 方法根据本地时间为一个日期对象设置月份。
13.1 语法
dateObj.setMonth(monthValue[, dayValue])
13.2 参数说明
- monthValue:介于 0 到 11 之间的整数(表示一月到十二月)。
- dayValue:可选。从 1 到 31 之间的整数,表示月份中的第几天。0 为上个月最后一天。
如果不指定 dayValue 参数,就会使用getDate方法的返回值。
如果有一个指定的参数超出了合理范围,setMonth 会相应地更新日期对象中的日期信息。例如,为 monthValue 指定 15,则年份会加 1,月份将会使用 3。
13.3 用法
let theBigDay = new Date();
theBigDay.setMonth(6);
14. Date.prototype.setHours()
setHours() 方法根据本地时间为一个日期对象设置小时数,返回从 1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。
14.1 语法
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
14.2 参数说明
- hoursValue:一个 0 到 23 的整数,表示小时。
- minutesValue:可选。一个 0 到 59 的整数,表示分钟。
- secondsValue:可选。一个 0 到 59 的整数,表示秒数。如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数。
- msValue:可选。一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValue 和 secondsValue 参数。
如果不指定 minutesValue,secondsValue 和 msValue 参数,则会使用getMinutes(),getSeconds() 和getMilliseconds() 方法的返回值。
如果有一个参数超出了合理范围,setHours 会相应地更新日期对象中的日期信息。例如,如果为 secondsValue 指定了 100,则分钟会加 1,然后秒数使用 40。
14.3 用法
let theBigDay = new Date();
theBigDay.setHours(7);
15. Date.prototype.setMinutes()
setMinutes() 方法根据本地时间为一个日期对象设置分钟数。
15.1 语法
dateObj.setMinutes(minutesValue[, secondsValue[, msValue]])
15.2 参数说明
- minutesValue:一个 0 到 59 的整数,表示分钟数。
- secondsValue:可选。一个 0 到 59 的整数,表示秒数。如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数。
- msValue:可选。一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValue 和secondsValue 参数。
如果没有指定 secondsValue 和 msValue 参数,就会使用 getSeconds() 和 getmilliseconds() 方法的返回值。
如果有一个指定的参数超出了合理范围,setMinutes 会相应地更新日期对象中的时间信息。例如,为 secondsValue 指定 100,分钟数将会加 1,而秒数会为 40。
15.3 用法
let theBigDay = new Date();
theBigDay.setMinutes(45);
16. Date.prototype.setSeconds()
setSeconds() 方法根据本地时间设置一个日期对象的秒数。
16.1 语法
dateObj.setSeconds(secondsValue[, msValue])
16.2 参数说明
- secondsValue:一个 0 到 59 的整数。
- msValue:一个 0 到 999 的数字,表示微秒数。
如果没有指定 msValue 参数,就会使用 getMilliseconds() 方法的返回值。
如果一个参数超出了合理范围, setSeconds 方法会相应地更新日期对象的时间信息。例如,为 secondsValue 指定 100,则日期对象的分钟数会相应地加 1,秒数将会使用 40。
16.3 用法
let theBigDay = new Date();
theBigDay.setSeconds(30);
17. Date.prototype.setMilliseconds()
setMilliseconds() 方法会根据本地时间设置一个日期对象的豪秒数。
17.1 语法
dateObj.setMilliseconds(millisecondsValue)
17.2 参数说明
- millisecondsValue:一个 0 到 999 的数字,表示豪秒数。
如果指定的数字超出了合理范围,则日期对象的时间信息会被相应地更新。例如,如果指定了 1005,则秒数加 1,豪秒数为 5。
17.3 用法
let theBigDay = new Date();
theBigDay.setMilliseconds(100);
18. Date.prototype.setTime()
setTime() 方法以一个表示从 1970-1-1 00:00:00 UTC 计时的毫秒数为来为 Date 对象设置时间。
18.1 语法
dateObj.setTime(timeValue)
18.2 参数
- timeValue:一个整数,表示从 1970-1-1 00:00:00 UTC 开始计时的毫秒数。
使用 setTime 方法用来把一个日期时间赋值给另一个 Date 对象。
18.3 用法
theBigDay = new Date("July 1, 1999");
sameAsBigDay = new Date();
sameAsBigDay.setTime(theBigDay.getTime());
19. 设置UTC时间的方法
设置UTC时间的方法其实也和本地时间的用法是一样的,只是方法名不同而已,具体语法可以参考上面本地时间。
- Date.prototype.setUTCDate():根据全球时间设置特定 date 对象的日期。
- Date.prototype.setUTCFullYear():根据世界标准时间为一个具体日期设置年份。
- Date.prototype.setUTCHours():根据世界标准时间为一个具体日期设置小时。
- Date.prototype.setUTCMilliseconds():根据世界时来设置指定时间的毫秒数。
- Date.prototype.setUTCMinutes():根据世界协调时(UTC)来设置指定日期的分钟数。
- Date.prototype.setUTCMonth():根据通用的时间来设置一个准确的月份。
- Date.prototype.setUTCSeconds():为一个依据国际通用时间的特定日期设置秒数。
20. Date.prototype.toDateString()
toDateString() 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串。
20.1 语法
dateObj.toDateString()
20.2 用法
ar d = new Date(1993, 6, 28, 14, 39, 7);
console.log(d.toString()); // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
console.log(d.toDateString()); // prints Wed Jul 28 1993
21. Date.prototype.toISOString()
toISOString() 方法返回一个 ISO(ISO 8601 Extended Format)格式的字符串: YYYY-MM-DDTHH:mm:ss.sssZ。时区总是 UTC(协调世界时),加一个后缀“Z”标识。
21.1 语法
dateObj.toISOString()
21.2 用法
let today = new Date("05 October 2011 14:48 UTC");
alert(today.toISOString()); // 返回 2011-10-05T14:48:00.000Z
22. Date.prototype.toJSON()
toJSON() 方法返回 Date 对象的字符串形式。
22.1 语法
dateObj.toJSON()
Date 实例引用一个具体的时间点。调用 toJSON() 返回一个 JSON 格式字符串 (使用 toISOString()),表示该日期对象的值。默认情况下,这个方法常用于 JSON序列化Date对象。
22.2 用法
let date = new Date();
console.log(date); //Thu Nov 09 2017 18:54:04 GMT+0800 (中国标准时间)
let jsonDate = (date).toJSON();
console.log(jsonDate); //"2017-11-09T10:51:11.395Z"
23. Date.prototype.toLocaleDateString()
toLocaleDateString() 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同。
23.1 语法
dateObj.toLocaleDateString([locales [, options]])
23.2 用法
23.2.1 使用locales
let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses month-day-year order
alert(date.toLocaleDateString("en-US"));
// → "12/19/2012"
// British English uses day-month-year order
alert(date.toLocaleDateString("en-GB"));
// → "20/12/2012"
// Korean uses year-month-day order
alert(date.toLocaleDateString("ko-KR"));
// → "2012. 12. 20."
// Arabic in most Arabic speaking countries uses real Arabic digits
alert(date.toLocaleDateString("ar-EG"));
// → "٢٠/١٢/٢٠١٢"
// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
alert(date.toLocaleDateString("ja-JP-u-ca-japanese"));
// → "24/12/20"
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(date.toLocaleDateString(["ban", "id"]));
// → "20/12/2012"
Copy to Clipboard
23.2.2 使用options
let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// request a weekday along with a long date
let options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
alert(date.toLocaleDateString("de-DE", options));
// → "Donnerstag, 20. Dezember 2012"
// an application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
alert(date.toLocaleDateString("en-US", options));
// → "Thursday, December 20, 2012, GMT"
24. Date.prototype.toLocaleTimeString()
The toLocaleTimeString() 方法返回该日期对象时间部分的字符串,该字符串格式因不同语言而不同。
24.1 语法
dateObj.toLocaleTimeString([locales [, options]])
24.2 用法
24.2.1 使用locales
let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses 12-hour time with AM/PM
alert(date.toLocaleTimeString("en-US"));
// → "7:00:00 PM"
// British English uses 24-hour time without AM/PM
alert(date.toLocaleTimeString("en-GB"));
// → "03:00:00"
// Korean uses 12-hour time with AM/PM
alert(date.toLocaleTimeString("ko-KR"));
// → "오후 12:00:00"
// Arabic in most Arabic speaking countries uses real Arabic digits
alert(date.toLocaleTimeString("ar-EG"));
// → "٧:٠٠:٠٠ م"
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(date.toLocaleTimeString(["ban", "id"]));
// → "11.00.00"
24.2.2 使用options
let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// an application may want to use UTC and make that visible
let options = {timeZone: "UTC", timeZoneName: "short"};
alert(date.toLocaleTimeString("en-US", options));
// → "3:00:00 AM GMT"
// sometimes even the US needs 24-hour time
alert(date.toLocaleTimeString("en-US", {hour12: false}));
// → "19:00:00"
25. Date.prototype.toString()
toString() 方法返回一个字符串,以本地的时区表示该 Date 对象。
25.1 语法
toString()
Date 对象覆盖了 Object 对象的 toString() 方法。Date.prototype.toString() 返回一个字符串,并以本地时区表示该 Date 对象,包含日期和时间——将 toDateString() 和 toTimeString() 通过一个空格拼接起来。
- 如果你只想获取日期,请使用 toDateString()。
- 如果你只想获取时间,请使用 toTimeString()。
- 如果你想要获取 UTC 时间而非本地时间,请使用 toUTCString()。
- 如果你想要以对用户更加友好的格式(例如,本地化)输出字符串,请使用 toLocaleString()。
25.2 用法
const x = new Date();
console.log(x.toString()); // Wed Sep 09 1998 05:36:22 GMT+0800 (中国标准时间)
26. Date.prototype.toTimeString()
toTimeString() 方法以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。
26.1 语法
dateObj.toTimeString()
26.2 用法
let d = new Date(1993, 6, 28, 14, 39, 7);
console.log(d.toString()); // Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
console.log(d.toTimeString()); // 14:39:07 GMT-0600 (PDT)
27. Date.prototype.toUTCString()
toUTCString() 方法把一个日期转换为一个字符串,使用 UTC 时区。
27.1 语法
dateObj.toUTCString()
toUTCString 的返回值是一个使用 UTC 时区的易读格式字符串。返回值的格式可能随平台而变化。
27.2 用法
let today = new Date();
let UTCstring = today.toUTCString();
// Mon, 03 Jul 2006 21:44:38 GMT
28. Date.prototype.valueOf()
valueOf() 方法返回一个 Date 对象的原始值。
28.1 语法
dateObj.valueOf()
valueOf 方法返回以数值格式表示的一个 Date 对象的原始值,从 1970 年 1 月 1 日 0 时 0 分 0 秒(UTC,即协调世界时)到该日期对象所代表时间的毫秒数。
该方法的功能和 Date.prototype.getTime() 方法一样。
28.2 用法
let x = new Date(56, 6, 17);
let myVar = x.valueOf(); // -424713600000