Javatpoint Logo
Javatpoint Logo

TypeScript Date Object

The Date object represents a date and time functionality in TypeScript. It allows us to get or set the year, month and day, hour, minute, second, and millisecond.

If we create a date without any argument passed to its constructor, by default, it contains the date and time of the user's computer.

The Date object also provides the functions which deal with Coordinated Universal Time (UTC) time, also known as Greenwich Mean Time (GMT). The World Time Standard is based on UTC time.

Creating Date Object

There are four ways to create a new date object:

1. new Date(): It creates a new date object with the current date and time.

Example

2. new Date(milliseconds): It creates a new date object as zero time plus milliseconds.

Example

3. new Date(datestring): It creates a new date object from a date string.

Example

4. new Date ( year, month, date[, hour, minute, second, millisecond ]): It creates a new date object with a specified date and time.

Example

Date Object Properties

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

Date Object Methods

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.

Example

output

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





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA