📜  打字稿日期对象

📅  最后修改于: 2021-01-11 12:53:39             🧑  作者: Mango

TypeScript日期对象

Date对象代表TypeScript中的日期时间功能。它使我们能够获取或设置年,月和日,时,分,秒和毫秒。

如果我们创建一个不带任何参数的日期,默认情况下,它将包含用户计算机的日期和时间。

Date对象还提供处理协调世界时(UTC)时间(也称为格林威治标准时间(GMT))的功能。世界时间标准基于UTC时间。

创建日期对象

有四种创建新日期对象的方法:

1. new Date():使用当前日期时间创建一个新的date对象。

let date: Date = new Date();
console.log("Date = " + date); //Date = Tue Feb 05 2019 12:05:22 GMT+0530 (IST)

2. new Date(milliseconds):创建一个新的日期对象,该对象为零时间加上毫秒

let date: Date = new Date(500000000000);
console.log("Date = " + date); //Date = Tue Nov 05 1985 06:23:20 GMT+0530 (IST)

3. new Date(datestring):它从日期字符串创建一个新的日期对象。

let date: Date = new Date("2019-01-16");
console.log("Date = " + date); //Date = Wed Jan 16 2019 05:30:00 GMT+0530 (IST)

4.新日期(年,月,日[,时,分,秒,毫秒]):它创建一个具有指定日期和时间的新日期对象。

let date: Date = new Date(2018, 0O5, 0O5, 17, 23, 42, 11);
console.log("Date = " + date); //Date = Tue Jun 05 2018 17:23:42 GMT+0530 (IST)

日期对象属性

Property Description
constructor It specifies the function that creates an object’s prototype.
prototype It allows to add properties and methods to an object.

日期对象方法

SN Method Description
1. Date() It is used to returns the current date and time.
2. getDate() It is used to returns day of the month for the specified date according to local time.
3. getDate() It is used to returns day of the week for the specified date according to local time.
4. getFullYear() It is used to returns year of the specified date according to local time.
5. getHours() It is used to returns hours in the specified date according to local time.
6. getMilliseconds() It is used to returns milliseconds in the specified date according to local time.
7. getMinutes() It is used to returns minutes in the specified date according to local time.
8. getMonth() It is used to returns month in the specified date according to local time.
9. getSeconds() It is used to returns seconds in the specified date according to local time.
10. getTime() It is used to returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
11. getTimezoneOffset() It is used to returns the time-zone offset in minutes for the current locale.
12. getUTCDate() It is used to returns the day(date) of the month in the specified date according to universal time.
13. getUTCDay() It is used to returns day of the week in the specified date according to universal time.
14. getUTCFullYear() It is used to returns the year in the specified date according to universal time.
15. getUTCHours() It is used to returns hours in the specified date according to universal time.
16. getUTCMilliseconds() It is used to returns milliseconds in the specified date according to universal time.
17. getUTCMinutes() It is used to returns the minutes in the specified date according to universal time.
18. getUTCMonth() It is used to returns the month in the specified date according to universal time.
19. getUTCSeconds() It is used to returns the seconds in the specified date according to universal time.
20. setDate() It is used to sets the day of the month for a specified date according to local time.
21. setFullYear() It is used to sets the full year for a specified date according to local time.
22. setHours() It is used to sets the hours for a specified date according to local time.
23. setMilliseconds() It is used to sets the milliseconds for a specified date according to local time.
24. setMinutes() It is used to sets the minutes for a specified date according to local time.
25. setMonth() It is used to sets the month for a specified date according to local time.
26. setSeconds() It is used to sets the seconds for a specified date according to local time.
27. setTime() It is used to sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
28. setUTCDate() It is used to sets the day(date) of the month for a specified date according to universal time.
29. setUTCFullYear() It is used to sets the full year in the specified date according to universal time.
30. setUTCHours() It is used to sets the hours for a specified date according to universal time.
31. setUTCMilliseconds() It is used to sets the milliseconds for a specified date according to universal time.
32. setUTCMinutes() It is used to sets the minutes for a specified date according to universal time.
33. setUTCMonth() It is used to sets the month for a specified date according to universal time.
34. setUTCSeconds() It is used to sets the seconds for a specified date according to universal time.
35. toDateString() It is used to returns the “date” portion of the date as a human-readable string.
36. toLocaleDateString() It is used to returns the “date” portion of the Date as a string, using the current locale’s conventions.
37. toLocaleFormat() It converts a date to a string, using a format string.
38. toLocaleString() It converts a date to a string, using the current locale’s conventions.
39. toLocaleTimeString() It is used to returns the “time” portion of the Date as a string, using the current locale’s conventions.
40. toSource() It is used to returns a string representing the source for an equivalent Date object; you can use this value to create a new object.
41. toString() It is used to returns a string representing the specified Date object.
42. toTimeString() It is used to returns the “time” portion of the Date as a human-readable string.
43. toUTCString() It converts a date to a string, using the universal time convention.
44. valueOf() It is used to returns the primitive value of a Date object.

let date: Date = new Date(2017, 4, 4, 17, 23, 42, 11);
date.setDate(13);
date.setMonth(13);
date.setFullYear(2013);
date.setHours(13);
date.setMinutes(13);
date.setSeconds(13);
console.log("Year = " + date.getFullYear());
console.log("Date = " + date.getDate());
console.log("Month = " + date.getMonth());
console.log("Day = " + date.getDay());
console.log("Hours = " + date.getHours());
console.log("Minutes = " + date.getMinutes());
console.log("Seconds = " + date.getSeconds());

输出

Year = 2013
Date = 13
Month = 1
Day = 3
Hours = 13
Minutes = 13
Seconds = 13