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:

new Date()

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

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

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

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

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

Example
snippet
let date: Date = new Date("2019-01-16");
console.log("Date = " + date); //Date = Wed Jan 16 2019 05:30:00 GMT+0530 (IST)
new Date ( year, month, date[, hour, minute, second, millisecond ])

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

Example
snippet
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)

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
snippet
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());
Output
Year = 2013 Date = 13 Month = 1 Day = 3 Hours = 13 Minutes = 13 Seconds = 13
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +